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;
}
}
你知道怎么做吗?
答案 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);
}
}