Using Time.deltaTime as a Timer
I've been using a LOT of timers for simple quick things and build them like so:
private float timer;
private float limit = 1f;
private bool doTimer = true;
if(doTimer)
{
timer += Time.deltaTime;
}
if(timer >= limit)
{
DoStuff();
timer = 0f;
}
My Question is if anybody can think of a reason why using timers this way is bad. I do know of MANY ways to make timers however this one is my favorite because it is so fast to set up and only needs to reset timer to 0 and/or set doTimer to false if you want to shut it down.
I know about using Invoke
Invoke("DoStuff", 1f);
but that will execute NO MATER WHAT without making other failsafes in place to stop it which can be annoying to sync up.
There is also
private float startTime;
private float timeSinceStartTime;
startTime = Time.time;
timeSinceStartTime = Time.time - startTime;
if(timeSinceStartTime >= limit)
{
DoStuff();
}
BBUUUUUTTT... Like I said, my brain likes the Time.deltaTime way and I want to see if anybody else out there can think of REAL reason this would be bad. OH, and of course, this is all run from inside Update. if instead this was inside FixedUpdate I would use:
timer += Time.fixedDeltaTime;
Thanks to everybody for reading.
Anybody out there? Still wondering about thoughts on this if anyone has them.
I'm working with State$$anonymous$$achineBehaviour which can't access coroutines/invoke, so for me, this is what I need to use, and your code is neat and tight!
Thanks, glad to know it is a useful reference!