Coroutine, Timer Local vs Class variable difference
Hello, I'm writing a shooting script, and i'm using Coroutine to delay my shots.
Here's the issue, i realize using delta time on a Class variable is inaccurate compared to using a Local variable. For some reason, the class variable timer is causing the gun to shoot faster.
Can some please explain to me why this is happening?
protected IEnumerator TimerCountdown()
{
float counter = timeBetweenShots;
while(counter > 0f)
{
counter -= Time.deltaTime;
yield return null;
}
canShoot = true;
}
This is the script using a Local variable timer.
protected IEnumerator TimerCountdown()
{
timer = timeBetweenShots;
while(timer > 0f)
{
timer -= Time.deltaTime;
yield return null;
}
}
This is the script using a Class variable timer.
Not sure if i'm doing something wrong here.
globalvariable.gif
(33.2 kB)
localvariable.gif
(19.1 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612160404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
is it possible to reset Time.time 1 Answer
Milliseconds Timer Question 1 Answer
(JS) Makeing demo version of game (15 minutes of demo) 0 Answers
I need the timer to reset as soon as i hits 0 1 Answer
How to do very accurate time measurement 3 Answers