Time.timeScale Unity 不工作

Time.timeScale not working Unity

这是两个使用 time.timeScale 的脚本。一个用它在我们 运行 超时时停止游戏,另一个用它在按下退出键时暂停游戏。 GameControlScript 运行 除了 Time.timeScale 是完美的。 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

        }

    }

}

}

一旦您将 timescale 设置为 0,所有对时间 deltaTime 的后续调用都将 return 为零(因此 timeRemaining 不会改变)。尝试使用 Time.unscaledDeltaTime 而不是

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

  1. 您需要确保加载游戏结束时暂停脚本不是 运行。或
  2. 只需将 paused 值设置为 true 这样它就可以工作并且不会将 time.timescale 重置为 1。

Time.timeScale 工作正常。

Unity、unreal 或其他游戏引擎(程序、应用程序)并没有您想象的"pause"。要冻结 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 将 return 0 并且您的对象将是 "freezed".

 void Start()
        {`
            Time.timeScale = 1;
        }

这对我有用。谢谢!