c# - 统一重生时重新启动计时器

标签 c# unity3d

当我在 Unity 中掉下悬崖时,我试图重新启动计时器。我已经有一个脚本可以让我在达到一定高度后在阈值处重生。我想做同样的事情,但不是重生,而是计时器重新启动。

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

    }
}

你知道怎么做吗?

最佳答案

你只需要在满足下降条件时重新启动定时器即可。 如果您在重生脚本中引用了计时器,那就太好了,例如:

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

    }
}

关于c# - 统一重生时重新启动计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42282796/

相关文章:

unity3d - 如何在运行时向 Unity Tilemap 添加可编写脚本的磁贴?

全息镜头中的 C++ dll

c# - 对以文件名作为参数的方法的测试和正确放置感到困惑

javascript - 发送 XMLHttpRequest 未与 Controller 方法绑定(bind)

ios - 为什么 Social.localUser.Authenticate 在 Unity 应用程序没有互联网连接时会导致崩溃?

c# - 我们如何使用统一在后台保存大量数据?

c# - 在 Unity 进程和另一个 C# 进程之间执行本地 IPC 的最快方法

c# - 在 GridView 中翻转项目图像

c# - 如何检查 LINQ to SQL 查询的结果?

c# - 使用用户名和密码时 RabbitMQ C# 连接问题