- Home /
Calculate in Seconds not Frames?
if (reloadTimer) {
reloadTimer -= Time.deltaTime;
}
if (coolTimer) {
coolTimer -= Time.deltaTime;
}
The problem is that the values don't stop at zero.
Answer by Memige · Oct 26, 2012 at 10:46 PM
Okay, a possible problem here is that Time.deltaTime won't necessarily land you on 0. It can, but it's far more likely to jump right past 0 to a negative number. Try this:
if (reloadTimer) {
reloadTimer -= Time.deltaTime;
if(reloadTimer < 0.0f)
{
reloadTimer = 0.0f;
}
}
if (coolTimer) {
coolTimer -= Time.deltaTime;
if(coolTimer < 0.0f)
{
coolTimer = 0.0f;
}
}
And this is if the reloadTimer and coolTimer are floats.
yes, Time.deltaTime returns the seconds since the last frame which will be a small decimal number (hopefully), so it will only be useful if you are using floats.
Yes, that was what kept happening. What is the point of putting 'f' behind the floats?
It casts the usual double (any number with a decimal) to a float. Just remove the "f" from the variable assignment and see what error you get.
Answer by DoctorWhy · Oct 24, 2012 at 11:04 PM
EDIT: Now that you completely changed the question, this answer is useless. But will leave it here anyway...
Integer if that is what you are doing. But that bases it off of the fps, not actual time. Maybe something like this: http://answers.unity3d.com/questions/11939/count-down-timer.html if you want accuracy.
Oh, woops... ignore that... misread your code... my dyslexia got the better of me...
Ok, so an int would be the better choice. What if I did use a float? How would that effect the game?
It wouldn't look like it effects the game at all to the player. However, if you had, say, a million floats that can be ints ins$$anonymous$$d, you may see lag because integer calculation speed is generally faster than floats.
And maybe this is different now-a-days with modern graphics cards and the speed of floating point math, which I hear is as fast, if not faster than integers. But, in general, if you don't need decimals, I wouldn't recommend using floats.