I get what it means when were updating timer variable to subtract and equal to time.deltaTime, because were just counting how much time it has been since the countdown has started or when timerValue has been called, but what I dont get is how we use timerValue. In the example below where were updating the fillFraction, but I dont get what it means to divide the timerValue by the timeToCompleteQuestion:
fillFraction = timerValue / timeToCompleteQuestion;
The next thing I dont understand is how were able to set the timerValue to the timeToShowCorrectAnswer but also have it equal to what time.deltaTime is?
timerValue = timetoShowCorrectAnswer;
If anyone can help me understand this it would be greatly appreciated!
Hi,
Thank you for your questions.
The name fillFraction
indicates that we are dealing with a fraction: x / y
. We use the fillFraction
to display an image. This means that we need values from 0 (0%) to 1 (100%).
At the start of our game, we execute this line of code: timerValue = timeToShowCorrectAnswer;
.
Let’s say that the value of timeToShowCorrectAnswer
is 30f. This means that timerValue
is 30f, too, at the start of our game. If timeToCompleteQuestion
is 30f as well, timerValue / timeToCompleteQuestion == 1f
because 30f / 30f == 1f
. fillFraction
would be 100%.
The next thing I dont understand is how were able to set the timerValue to the timeToShowCorrectAnswer but also have it equal to what time.deltaTime is?
We do not set it equal to Time.deltaTime
. We subtract Time.deltaTime
from the value.
timerValue -= Time.deltaTime;
is the same as timerValue = timerValue - Time.deltaTime;
. Programmers don’t like to type lengthy code, so we have a lot of so-called syntactic sugar.
Each frame, we execute this line: timerValue -= Time.deltaTime;
. This means that our fillFraction
value decreases each frame.
Did this clear it up for you?
See also:
- Forum User Guides : How to mark a topic as solved
Yes! Thank you for explaining this to me, I didn’t understand that it was a fraction and now knowing this makes it way easier! One thing I just want to clear up though is if since were doing timerValue -= time.deltaTime
than its different than when were just setting timerValue = timeToShowCorrectAnswer
or timeToCompleteQuestion
because one is subtracting and one is making equal but sometimes both run at the exact same time.
They do not run at the same time. timerValue -= time.deltaTime
gets executed first and always (if Update gets called). timerValue = {something}
gets executed afterwards but only if the if-conditions are true. And if they are true, timerValue -= time.deltaTime
is irrelevant for the following logic because the value of timerValue
gets overridden.
Or is this what you meant?
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.