当我在 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/