Timer in Coroutines not working
So here is my timer., i am running it in a coroutine
//shoot some stuff over time
//maxBulletDelay is a constant 0.01f
thisBody.velocity = Vector3.zero;
int numberOfBulletsFired = 0;
for (int i = 0; i < numberOfBulletsToFire; i++)
{ //TOP MOST IF STARTS HERE
if (isPaused.Value) { //pausing trap, dont remove.
while (isPaused.Value)
yield return null;
}
for (float timer = 0.0f; timer < maxBulletDelay; timer += Time.deltaTime) {
yield return null;
}
IBullet thisBullet = enemyBulletPool.GetBulletToSimulate (BULLET_TYPE.PBT_YELLOW, 0.0f);
if (thisBullet != null) {
//do firing of bullet...
}
} //TOP MOST IF STARTS ENDS
//..rest of coroutine stuff
I run it on my laptop, and the thing firing the bullets looks like this:
When i run it on my desktop, the bullet streams are shorter, which is telling me that there is some framerate dependency in this timer somewhere, even though it uses time.deltaTime. There was even an instance where on my desktop all the bullets came out in 10 consecutive frames
My question is, is this the best way to do a timer in a coroutine so its deterministic and frame rate independent? I tried this code off this forum post: https://forum.unity3d.com/threads/coroutines-and-lag-low-fps.336689/#post-2178723
for (float timer = 0.0f; timer < maxBulletDelay; timer += Time.fixedDeltaTime) {
yield return new WaitForFixedUpdate;
}
and it seems to do the trick, but I am not sure what is going on. I figure it has to do with the time.fixedDeltaTime being updated more regularly, so the time resolution is more accurate, but could someone please explain the do/don'ts of timers in coroutines and where i am going wrong with this.
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220612120532im_/https://answers.unity.com/themes/thub/images/avi.jpg)