- Home /
Cant Clamp
My goal is to have a countdown timer that counts down to zero and stays at zero, however no matter what I try, it always continues counting down past zero. Currently, i have a text GameObject linked to displayCount via the inspector.
MY CODE:
public class CountDown : MonoBehaviour {
private float counter;
Text Count;
public GameObject displayCount;
void Start()
{
Count = displayCount.GetComponent<Text>();
counter = 10f;
}
void Update ()
{
counter -= Mathf.Clamp(Time.deltaTime, 0.0f, 10f);
Count.text = counter.ToString(); }
}
Answer by TreyH · May 06, 2018 at 08:21 PM
You're clamping the wrong thing:
counter -= Time.deltaTime;
counter = Mathf.Clamp(counter, 0.0f, 10f);
It is worth noting that my original problem was that I was applying Time.deltaTime after clamping which somehow overrides the clamp. Only by clamping afterward does the counter get clamped, it seems.
Well, there wasn't any weird magic overriding something else. The issue was that you were clamping the value being added to your counter, but imposing no restrictions on the counter itself.
If I were to clamp your paycheck each month between 0 and 10 dollars, then that would only restrict the amount of money you would receive per month. It doesn't have any influence on how much money you can save over an unlimited number of months, though.
Your answer
Follow this Question
Related Questions
make a countdown timer 1 Answer
timer not ticking down 2 Answers
How do I make the timeScale not affect the timer? 1 Answer
Trying to make a timer work.. 0 Answers
Distribute terrain in zones 3 Answers