我的倒数计时器卡住了,游戏没有停止

我正在尝试创建一个倒计时计时器,以便我的玩家在游戏再次开始玩之前购买第二条生命 3 秒。我不确定我需要在下面修复什么才能使其正常工作。目前,所有用户界面都会在应有的时候弹出,倒数计时器文本也会弹出,但游戏继续进行,计时器停留在 3。


public static bool GameOver = false;

public static bool CountDown = false;


public GameObject deathMenuUI;

public GameObject countDownUI;


public int timeLeft = 3;

public Text countDownText;


public float slowness = 10f;


public void Start()

{

    PlayerPrefs.SetInt("Score", 0);

    Time.timeScale = 1f;

}


public void EndGame()

{

    StartCoroutine(DeathScreen());

}

//on death, slow time for one second and open deathscreen UI

IEnumerator DeathScreen()

{

    Time.timeScale = 1f / slowness;

    Time.fixedDeltaTime = Time.fixedDeltaTime / slowness;


    yield return new WaitForSeconds(1f / slowness);


    Time.timeScale = 1f;

    Time.fixedDeltaTime = Time.fixedDeltaTime * slowness;


    deathMenuUI.SetActive(true);

    Time.timeScale = 0f;

    GameOver = true;

}

//if game is continued, close deathUI and start timer

public void continueGame()

{

    deathMenuUI.SetActive(false);

    Time.timeScale = 1f;

    GameOver = false;

    StartCoroutine(LoseTime());

}

private void Update()

{

    countDownText.text = ("" + timeLeft);

    if (timeLeft <= 0)

    {

        StopCoroutine(LoseTime());

    }

}

IEnumerator LoseTime()

{

    while (true)

    {

        countDownUI.SetActive(true);

        CountDown = true;

        yield return new WaitForSeconds(1);

        timeLeft--;

    }

}

}


月关宝盒
浏览 310回答 1
1回答

aluckdog

由于您设置Time.timeScale = 0暂停游戏,因此您需要使用WaitForSecondsRealtime而不是WaitForSeconds(就像@Prodian 在评论部分建议的那样),因为WaitForSecondsRealtime使用未缩放的时间,无论您设置什么值都不会受到影响Time.timeScale。我在这里对您的代码进行了一些修改://if game is continued, close deathUI and start timerpublic void continueGame(){&nbsp; &nbsp; // Add this line to prevent accident like double click... which will start multiple coroutine and cause unexpected result&nbsp; &nbsp; if (CountDown) return;&nbsp; &nbsp; CountDown = true;&nbsp; &nbsp; deathMenuUI.SetActive(false);&nbsp; &nbsp; // Since you want your game to still being paused for 3 seconds before resuming&nbsp; &nbsp; Time.timeScale = 0;&nbsp; &nbsp; StartCoroutine(LoseTime());}private void AfterCountdownFinished(){&nbsp; &nbsp; // important. You must set your Time.timeScale back to its default value&nbsp; &nbsp; // because even if you reload your scene the timeScale remain the same which can cause you to encounter freeze error which you might spend time to search for the problem&nbsp; &nbsp; Time.timeScale = 1;&nbsp; &nbsp; GameOver = CountDown = false;&nbsp; &nbsp; countDownUI.SetActive(false);&nbsp; &nbsp; // You want to write your restart/continue logic here&nbsp; &nbsp; // Example:&nbsp; &nbsp; // reload the level&nbsp; &nbsp; // SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}IEnumerator LoseTime(){&nbsp; &nbsp; // I forgot this part (which you pointed out)&nbsp; &nbsp; countDownUI.SetActive(true);&nbsp; &nbsp; // Here is how you can improve performance&nbsp; &nbsp; // It seems you can't create and reuse it like WaitForSeconds&nbsp; &nbsp; // var delay = new WaitForSecondsRealtime(1);&nbsp; &nbsp; // Set this the text here so it will display (3 in your case) for 1 second before start decreasing&nbsp; &nbsp; countDownText.text = timeLeft.ToString();&nbsp; &nbsp; // If your level will restart after the countdown ends then you don't need to create another variable like below. You can just use timeLeft directly&nbsp; &nbsp; int time = timeLeft;&nbsp; &nbsp; while (time > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Edited here&nbsp; &nbsp; &nbsp; &nbsp; yield return new WaitForSecondsRealtime(1);&nbsp; &nbsp; &nbsp; &nbsp; time--;&nbsp; &nbsp; &nbsp; &nbsp; // Here you don't set your UI text every frame in the update but you only set it only when there is change on timeLeft&nbsp; &nbsp; &nbsp; &nbsp; countDownText.text = time.ToString();&nbsp; &nbsp; }&nbsp; &nbsp; // When countdown ended. You don't need to call StopCoroutine&nbsp; &nbsp; AfterCountdownFinished();}如果您的 UI 有某种Time.deltaTime用于制作动画的动画,那么您需要将其更改为使用Time.unscaledDeltaTime。它与WaitForSecondsand 的想法相同WaitForSecondsRealtime,一个受Time.timeScale值影响而另一个不受值影响。PS:我Update完全删除了你的功能,因为如果你按照我的方式实现你就不需要它了。而且你不需要在StopCoroutine任何地方打电话,因为倒计时结束时会自动停止
打开App,查看更多内容
随时随地看视频慕课网APP