创建一个类似Time.time的计时器,但可以将其重置为0

时间:2017-05-25 09:54:07

标签: c# unity3d

我可以制作计时器或以某种方式重置Time.time吗?我试过这个

    float Timer(float time)
    {
        if (Time.time > nextTime)
        {
            return 0.01f;
            nextTime = Time.time + 0.01f;
        }
        else
        {
            return 0;
        }
    }

但它似乎无法正常工作,时间不算与Time.time相同

2 个答案:

答案 0 :(得分:0)

您可以像这样创建计时器

  public float timeLeft=5f;

     void FixedUpdate()
     {
         timeLeft -= Time.deltaTime;
         if(timeLeft < 0)
         {
             DoSomething();
             timeLeft=5f; //If you want to reset timer 
         }
     }

答案 1 :(得分:0)

public class Timer : MonoBehaviour
{
    private static float _lastTime = Time.realtimeSinceStartup;

    [SerializeField, Tooltip("Time in second between timer ding dong."), Range(0f, 3600f)]
    private float _interval = 5f;

    public UnityEvent OnTickTack;

    private void Update()
    {
        if (Time.realtimeSinceStartup - _lastTime >= _interval)
        {
            _lastTime = Time.realtimeSinceStartup;
            OnTickTack.Invoke();
        }
    }
}

或者你总是可以使用coroutine“yield return new WaitForSeconds(5f);”