[英]How do a make a countdown timer reset or go back up by a few seconds?
我試圖讓這個時間 go 及時上升 5 秒,或者在我輸入觸發器時重置。
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Timer2 : MonoBehaviour
{
public float timeLeft = 3.0f;
public Text startText; // used for showing countdown from 3, 2, 1
void Update()
{
timeLeft -= Time.deltaTime;
startText.text = (timeLeft).ToString("0");
if (timeLeft < 0)
{
if (timeLeft < 0)
{
SceneManager.LoadScene("GameOver");
}
}
您是否嘗試過簡單地添加到timeLeft
變量?
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Timer2 : MonoBehaviour
{
public float timeLeft = 3.0f;
public Text startText; // used for showing countdown from 3, 2, 1
void Update()
{
timeLeft -= Time.deltaTime;
startText.text = (timeLeft).ToString("0");
if (timeLeft < 0)
{
if (timeLeft < 0)
{
SceneManager.LoadScene("GameOver");
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
//check to see if the collision is correct
timeLeft += 5; //parameterize this number if needed
}
}
如果您想重置timeLeft
,只需替換timeLeft += 5;
使用timeLeft = 3.0f
(如果要這樣做,您可能希望將初始值移動到變量中)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.