【发布时间】:2018-08-03 19:40:59
【问题描述】:
我正在尝试创建一个倒计时计时器,用于当我的玩家在游戏再次开始前 3 秒购买第二条生命时。我不确定我需要在下面修复什么才能使其正常工作。目前,所有的 UI 都会在应有的时候弹出,倒数计时器文本也确实会弹出,但游戏继续进行,并且计时器停留在 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--;
}
}
}
【问题讨论】:
-
你的Deathscreen只等待100ms然后GameOver...这是故意的吗?
-
实际上是 1 秒。它会在死亡时产生子弹时间效果,从而减慢时间:)
-
试着把 Debug.Log 放到你的 IEnumerator LoseTime() 里面看看协程是否真的被触发了
-
而且我没有看到您在代码中的任何位置重置
timeLeft变量,您应该在continueGame()函数中执行此操作,以便在您第一次调用continueGame()后您的倒计时获胜'不要像你想要的那样从小于或等于 0 而不是 3 的数字开始 -
如果你想同时使用waitForSeconds和TimeScale 0,你应该使用“yield return new WaitForSecondsRealtime(time);”。 WaitForSecondsRealtime 不依赖于 timeScale,它依赖于计算机的时钟。