首页 > 解决方案 > 每次执行启动倒计时时,冷却计时器的速度都会加倍

问题描述

当玩家死亡时,一颗心会从生命条中移除。一个冷却计时器启动,将在 5 分钟后重新填充心脏。如果玩家再次死亡,第二颗心脏将被移除,但计时器将首先完成当前的冷却,然后重新开始。

当玩家第二次死亡时,场景会重新加载并显示倒计时,让玩家有时间为下一次尝试做准备。

问题是一旦倒计时完成并且时间刻度设置为 1,冷却计时器速度就会增加。如果他再次死亡,计时器速度再次增加,直到第一个冷却计时器完成然后重置并开始下一个冷却,然后速度恢复正常,或者如果我退出游戏并重新进入计时器恢复正常.

当场景加载时,时间刻度为 0。倒计时动画使用 Mycoroutine 脚本运行。动画完成后,时间刻度设置回 1。

冷却计时器考虑了玩家使用 TimerMaster 脚本离线的时间,并且 100% 工作。在 android 上对其进行了测试,如果游戏移到后台,计时器工作正常,使用 Time.unscaledDeltaTime 而不是 Time.DeltaTime。

我想不通的唯一问题是每次重新启动关卡时速度都会增加。

//this is used to call the countdown
public bool setCoundown = false;

void Start()
{
    countDown.SetActive(true);  //when active it sets setCountdown to true
    Time.timeScale = 0;
}

void Update()
{
    if (setCoundown)
    {
        StartCoroutine(CountDown()); //set the timer to wait till the countdown is done
    }
}

IEnumerator CountDown()
{
    while (setCoundown == true)
    {
        yield return StartCoroutine(Mycoroutine.WaitforRealSeconds(0.5f));
        setCoundown = false;
        Time.timeScale = 1;
        RunGame();
    }
}

//This script is called by CountDown()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public  static class Mycoroutine
{
    public static IEnumerator WaitforRealSeconds(float time)
    {
        float start = Time.realtimeSinceStartup;

        while(Time.realtimeSinceStartup < (start + time))
        {
            yield return null;
        }
    }    
}

//This is the cool down timer

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

public class TimerText : MonoBehaviour
public static TimerText instance;

[SerializeField]
private Text timeText;

private int health;
private float timer, minutes, seconds;

void Start()
{

    //get saved time that was needed to fill all health
    timer = GameController.instance.GetTimerTime();

    //deduct the offline time from the needed time
    timer = timer - TimerMaster.instance.CheckDate();

    //call the function to calculate offline time and set lives and timer value accordingly if the health is full then the timer value will be 300
    DateTimer();                                                                
}

void Update()                                                                   
{           
    health = GameController.instance.GetHealth();

    if (health < 3 && timeText.text == "0:00")
    {
        StartCoundownTimer();
    }
}

void StartCoundownTimer()                                                       
{
    if (timeText != null)
    {
        timeText.text = "0:00";
        InvokeRepeating("UpdateTimer", 0.0f, 0.01667f);
    }
}

void UpdateTimer()
{
    if (timeText != null)
    {
        timer -= Math.Max(Time.unscaledDeltaTime, 0);
        timeText.text = string.Format("{0}:{1:00}", (int)(timer / 60), (int)(timer % 60));


        if (timeText.text == "0:00")
        {
            CancelInvoke();
            health = GameController.instance.GetHealth() + 1;
            GameController.instance.SetHealth(health);
            timer = 300;
        }
    }
}

标签: c#unity3d

解决方案


推荐阅读