在respawn上重启计时器

时间:2017-02-16 19:18:42

标签: c# unity3d

当我在Unity的悬崖上摔倒时,我试图重新启动计时器。我已经有了一个脚本,让我在一定高度后重新生成阈值。我想做同样的事情,但不是重生,计时器重新启动。

constexpr

我的重生脚本在我的相机装备上,它是

public class Timer : MonoBehaviour {

     public Text timerText;
     private float startTime;
     private bool finished = false;
     private bool started = false;

     void Update () 
     {      
         if(!started || finished)
             return;

         float t = Time.time - startTime;

         string minutes = ((int) t/60).ToString();
         string seconds = (t%60).ToString("f2");

         timerText.text = minutes + ":" + seconds;      
     }

     public void StartTimer ()
     {
         started = true;
         startTime = Time.time;
     }

     public void StopTimer()
     {
         finished = true;
         timerText.color = Color.yellow;      
     }   
 }

你知道怎么做吗?

2 个答案:

答案 0 :(得分:4)

您只需在遇到下降情况时重新启动计时器。 如果您在重新生成的脚本中引用了Timer,那就太好了,例如:

public class respawn : MonoBehaviour
{
    public Timer timer;
    public float threshold;

    void FixedUpdate()
    {
        if (transform.position.y < threshold)
        {
        //transform.position = new Vector3(403, 266, 337);
        timer.StartTimer();
        }

    }
}

答案 1 :(得分:3)

只需再次调用StartTimer()并在方法中重置完成:

 // ...
 public void StartTimer ()
 {
     finished = false;
     started = true;
     startTime = Time.time;
 }

 public void StopTimer()
 {
     finished = true;
     timerText.color = Color.yellow;      
 } 

不要忘记在重新生成的脚本中停止计时器:

public Timer Timer;

void FixedUpdate()
{
    if (transform.position.y < threshold)
    {
        Timer.StopTimer();
        transform.position = new Vector3(403, 266, 337);
    }

}