Time.timeScale不工作Unity

时间:2018-05-10 01:01:20

标签: unity3d game-development

这两个脚本使用time.timeScale。当我们用完时间时,一个使用它来停止游戏,而当按下逃脱时,另一个使用它来暂停游戏。除Time.timeScale外,GameControlScript运行完美。 Gameover GUI显示但游戏仍然在GUI后面继续。

//This script is for game mechanics, this script controls point system, gameover, score, total time in the game

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameControlScript : MonoBehaviour {

//  public GUISkin skin;
float timeRemaining = 10; //time left at start of game
float timeExtension = 3f; //time added after powerup is collected
float timeDeduction = 2f; //time deducted after obsticle is hit
float totalTimeElapsed = 0; //time you last in game
float score=0f; //player's score
public bool isGameOver = false; //flag to check if game is over
public AudioSource GameOverSound; //sound play at the end game


public Text ScoreBoard;
public Text FinalScore;
public Text GameTimeLeft;
public GameObject GameOverPanel;


 void Start(){
    Time.timeScale = 1;  // set the time scale to 1, to start the game world. This is needed if you restart the game from the game over menu
}

 void Update () { 



    if(isGameOver) //check if game is over
        return; //if game is not over don't do anything (it sends control to Update and doesn't let it excute code down there)



        totalTimeElapsed += Time.deltaTime; //adding amount of time passed in totalTimeElapsed variable
        score = totalTimeElapsed * 100; //multipling totalTimeElapsed with 100 to create score of player
        timeRemaining -= Time.deltaTime; //subtracting passing time from timeRemaining

        GameTimeLeft.text = "TIME LEFT: " + ((int)timeRemaining).ToString ();
        ScoreBoard.text = "SCORE: " + ((int)score).ToString ();

    if (timeRemaining <= 0) { //check if timeRemaining is less or equal to zero


        Time.timeScale = 0;

        GameOverPanel.SetActive (true);
        FinalScore.text = "YOUR SCORE: " + ((int)score).ToString ();

        isGameOver = true; //if it is less or equal to zero game is over
        GameOverSound.Play (); //play gameover sound

    }//end if

    }//end Update

这是暂停脚本:

//This Script pause the game when escape is pressed
using UnityEngine;

using System.Collections;

public class PauseMenuScript : MonoBehaviour

{

public GUISkin myskin; //custom GUIskin reference

public string levelToLoad;// name of level to load

public bool paused = false;  //flag to check if the game is paused

public AudioSource MenuClickSound;




private void Start()

{

    Time.timeScale=1; //Set the timeScale back to 1 for Restart option to work

}



private void Update()

{



    if (Input.GetKeyDown(KeyCode.Escape) ) //check if Escape key/Back key is pressed

    {

        if (paused)

            paused = false; //unpause the game if already

        else

            paused = true; //pause the game if not paused

    }

    if(paused)

        Time.timeScale = 0; //set the timeScale to 0 so that all the procedings are halte

    else

        Time.timeScale = 1; //set it back to 1 on unpausing the game



}



private void OnGUI()

{

    GUI.skin=myskin;  //use the custom GUISkin

    if (paused){   


        //Shows text PAUSED when game is paused
        GUI.Box(new Rect(Screen.width/4, Screen.height/4, Screen.width/2, Screen.height/2), "PAUSED");



        //Draws button Resume and resume game when it is pressed
        if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESUME")){
            MenuClickSound.Play ();
            paused = false;

        }
        //Draws button Restart and restart game when it is pressed
        if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+2*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESTART")){
            MenuClickSound.Play ();
            Application.LoadLevel(Application.loadedLevel); // load already loaded level

        }


        //Draws button Main Menu and takes user to main menu when it is pressed
        if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+3*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "MAIN MENU")){
            MenuClickSound.Play ();
            Application.LoadLevel(levelToLoad); // name of the scene which has to be load in order to go to main menu

        }

    }

}

}

3 个答案:

答案 0 :(得分:2)

这是导致问题的暂停脚本。 当您的游戏正在运行时,暂停设置为false,因此它会在更新时将时间刻度重置为1,因此当调用游戏时,暂停仍然是假的。

  1. 您需要确保在加载游戏时暂停脚本不运行。或
  2. 只需将暂停值设置为true即可正常工作,并且不会将time.timescale重置为1。

答案 1 :(得分:1)

Time.timeScale效果很好。

Unity,虚幻或其他游戏引擎(程序,应用程序)没有一些&#34;暂停&#34;如你想象的。要冻结GUI后面的某些对象,必须将(例如)moveSpeed乘以deltaTime。

示例:

public class CharacterController:MonoBehaviour{
    public float moveSpeed = 3.5f; 
    void Update(){
       transform.position += Vector3.Right * moveSpeed * Time.deltaTime;
    }
}

Time.timeScale = 0;然后Time.deltaTime将返回0并且您的对象将被&#34;冻结&#34;。

答案 2 :(得分:0)

一旦将时间刻度设置为0,对时间deltaTime的所有后续调用都将返回零(因此timeRemaining不会更改)。请尝试使用Time.unscaledDeltaTime