# Timer nightmare with different game speeds

Hi All,

I am working on a 2.5D tower game and i recently had to go back to something that i thought fixed and well working already long ago.
In my game there are 4 possible game speeds (i might reduce it to 3 later, but for now it’s good to also have a very fast one to speed up visual debugging) : x1, x3, x5 and x10.
The problem is that if I test a single wave with speed x1 and a single tower shooting at the wave, the outcome is always identical (same amount of killed ones, same amount of hurt ones, same amount of getting away unhurt). The same happens for x3 speed and x5 speed (that means testing several times the same way with same tower and same speed).

At that point comes out the problem. The x10 wave always has a small difference in results, slightly ones, but the outcome of multiple tests on the same wave, tower and speed isn’t identical as i would expect (there is absolutely no random factors in hitting, damage, health, etc).
But more than that, the big problem comes from the fact that even if differences are small, the x1, x3, x5 and x10 speed results are different from each other.

That should not obviously happen as with each speed the outcome should be identical with the same setup. I suspect the problem might be in the approximations on how the timers are modified due to the game speed.

At first i was having the game speed modifying the timer this way :

``````subTimer-=Time.deltaTime*gameSpeed;
``````

but then i found out that the results seemed to improve if i was instead dividing the timervalue when the timer was reset like this :

``````//reloading timer between two shots
if(timer<=0){
timer = (double)((fireRate/(1f+fireRateTempMod+skillFirerateMod+heroFirerateMod))/gameSpeed);
}
else{
//i tried here both deltaTime and smoothDeltaTime but with no visible differences in results
timer-=(double)(Time.smoothDeltaTime);
}
}
``````

The framerate of the game oscillates constantly between 62 to 68 fps, never dropping below 60. I printed out the shooting times of the tower and somehow there is an oscillation in duration and frames between the shots, but that should be due to the variable length of deltaTime i suppose.

I generate the units for the path with exactly the same method (so the spawning should speed up correctly with the speed changing). The only thing that is being managed differently somehow is the movement of the units. (the comment in the code explains why i divide the speed into x and y components).

``````        //with respect to the map simulated perspective, split the units movement into x and y component, and the y component is reduced to 75%
yPercent = 100f/(Mathf.Abs((nextWPPosition-unitMain.position).y)+Mathf.Abs((nextWPPosition-unitMain.position).x))*Mathf.Abs((nextWPPosition-unitMain.position).y);
xPercent = 100f/(Mathf.Abs((nextWPPosition-unitMain.position).y)+Mathf.Abs((nextWPPosition-unitMain.position).x))*Mathf.Abs((nextWPPosition-unitMain.position).x);

ySpeed = speed*(yPercent/100f);
xSpeed = speed*(xPercent/100f);

Vector3 tempVec = Vector3.MoveTowards(unitMain.position, nextWPPosition, (ySpeed*0.75f+xSpeed)*(modS_1+modSpeed_2)*Time.deltaTime*30f*gameSpeed);
``````

I know there might be some other problem spread all over the code (the game starts to be reasonably big), but i spent the last two days hunting for this bug and i think it’s time that i ask for other unity-ers opinions. Maybe something obvious that i don’t see or know comes to your mind if you had the patience of reading my whole post
If you have any other idea on how to manage a more reliable timer system, please don’t hesitate to let me know!

Thanks and best,
H

On further experimenting, I now tried to skip completely the use of my gamespeed variables and i tried using instead timescale (stupid me that i didn’t try in the beginning as it would have been much much easier to use), but still, similar problems.
I tried also different speeds, now in the test i have x1,x2,x3,x5 and x8 and even by changing only the timescale i have the same problem.
Only x2 gives the same results as x1 (probably being the difference so minimal).
I hope this might give you some further hint to give me some ideas, cause i am banging my head on the keyboard but with no solutions.

Best,
H

Hello!

You can keep messing with Time.deltaTime, but almost all code that relies on this will break at some point. In your experience, this becomes blatantly obvious at 10x speed. What would probably work better is to run the simulation 10 times in a row instead of multiplying deltaTime by 10.

Even harder, but to make your game fully deterministic, is to completely separate your game logic from rendering, then make every game step use the exact same deltaTime all the time. 10x speed runs through 10 game steps without rendering anything to the screen.

http://gafferongames.com/game-physics/fix-your-timestep/

Is this a physics based game?

What was sub-timer used for?

Its not clear to me what the problems are… perhaps you can elaborate.

timescale is definitely the smarter choice for speeding up a game… Ive used it in a physics based game and, while the physics was pretty simple, the results have been consistent no matter how fast I played it.

Hi guys,

@GarthSmith : Definitely a good insight in the problem, possibly the one i was exactly looking for. (i feel extremely naive to don’t have realized that already). I have a couple of questions, but before asking stupidly, i will read in detail the two articles. Thanks!

@JamesLeeNZ : There is no physics in the game (just collisions if that can be considered physics). Sub-timer is being used to determine the spawning speed of the enemies. So basically i have four main things being influenced by time : units spawning, units movement, tower firerate and additionally the movement of the arrow (that simulates a 3d trajectory on the 2d space). I also had hopes in Timescale when i tried yesterday, but as i said the problem seems to remain and being quite similar by using the timescale, so i think the determinism of the game might be due to the same problems mentioned by Garth. Did you have to take any special/specific measure to change the timescale in your game?

Thanks to both,
Best,
H

Ok, I tried to apply the concept of the first link, and the fastest way to do it seemed to be by imposing the fixed timestep to 0.016ms and then change all my Update() functions (aside from those managing the player input) into FixedUpdate() and all the Time.deltaTime to Time.fixedDeltaTime. Then to control the game speed i just used the timescale.

And it works!! Now at all the different speeds the outcome of the test is always identical.

One last question, is that a correct behavior this “misusing” of the FixedUpdate() or might it cause further problems in the future? I am fairly sure that my fps shouldn’t drop below 60 fps (most of the larger calculations are already implemented), but is there some other possible problem i should know about?

Thanks again for saving my brain,
Best,
H