加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用unity重新启动计时器

发布时间:2020-12-15 23:37:32 所属栏目:百科 来源:网络整理
导读:当我在Unity中摔倒时,我正试图让Timer重启.我已经有了一个脚本,让我在一定高度后重新生成阈值.我想做同样的事情,但不是重生,计时器重新启动. public class Timer : MonoBehaviour { public Text timerText; private float startTime; private bool finished
当我在Unity中摔倒时,我正试图让Timer重启.我已经有了一个脚本,让我在一定高度后重新生成阈值.我想做同样的事情,但不是重生,计时器重新启动.

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;      
     }   
 }

我的重生脚本在我的相机装备上,就是这样

public class respawn : MonoBehaviour
{
    public float threshold;

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

    }
}

你知道如何制作这个吗?

解决方法

您只需在满足下降条件时重新启动计时器.
如果您在重新生成的脚本中引用了Timer,那将会很好,例如:

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

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

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读