【问题标题】:Time.timeScale not working UnityTime.timeScale 不工作统一
【发布时间】:2018-10-20 04:51:18
【问题描述】:

这是两个使用 time.timeScale 的脚本。一个用它在我们时间用完时停止游戏,另一个用它在按下 Escape 时暂停游戏。 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

        }

    }

}

}

【问题讨论】:

  • “它不起作用”....你需要解释一下你的意思
  • 也许是因为timeRemaining &lt;= 0 从来都不是真的?
  • 如果您有两个脚本都与Time.timeScale 混在一起,一个 获胜。
  • @z3nth10n timeRemaining
  • @Draco18s 两个脚本没有同时运行。如果我按下退出,暂停脚本将运行,如果游戏时间用完,它将停止一切并显示游戏结束和最终得分的 GUI。但是时间刻度在此脚本中不起作用,但如果我按转义键,则会在暂停脚本中运行。

标签: unity3d game-development


【解决方案1】:

是您的暂停脚本导致了问题。 当你的游戏运行时,paused 设置为 false,所以它在更新时将 timescale 重置为 1,所以当调用 gameover 时,paused 仍然是 false。

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

【讨论】:

  • 但我猜你的问题是,一旦加载了gameover,你的游戏还在后台运行,对吧?这是因为 time.timescale 是 1 而不是 0 所以你需要更正它。
  • @SKJ DING DING DING 你和 Draco18 是对的。谢谢!
  • @Draco18s 谢谢。我禁用了暂停脚本,它可以工作。调试在暂停脚本中记录 else 语句。事实证明它正在按照 SKJ 所说的那样做。
  • ...或what I said...
  • @NoorulainArain 我很高兴它有帮助。请接受它作为正确答案。
【解决方案2】:

一旦将 timescale 设置为 0,所有后续对 time deltaTime 的调用都将返回零(因此 timeRemaining 不会改变)。尝试改用 Time.unscaledDeltaTime

【讨论】:

  • timeRemaining 工作正常。其中的代码效果很好。只有 Time.timeScale 不起作用。
【解决方案3】:

Time.timeScale 工作正常。

Unity、虚幻或其他游戏引擎(程序、应用程序)并不像您想象的那样有一些“暂停”。要冻结 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 并且您的对象将被“冻结”。

【讨论】:

    【解决方案4】:
     void Start()
            {`
                Time.timeScale = 1;
            }
    

    这对我有用。谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      相关资源
      最近更新 更多