- Home /
Time.deltaTime does not always correctly display the time difference between 2 frames
I have a situation where Time.deltaTime and Time.time do not correctly update between Update() calls.
At one point, I am loading a lot of game objects, something which takes about 2 seconds, during which the game basically freezes. This is ok - I will optimize this later. When I log the updated time between the last frame before the freeze and the first frame after the freeze, I get:
2014-01-29 11:30:14.3543 We increase time by Time.deltaTime 0.01639664 to Time.time 11.4154 // 2 second freeze here [info] 2014-01-29 11:30:16.6814 We increase time by Time.deltaTime 0.3333333 to Time.time 11.74873
If I log the time using DateTime.Now(), I get the correct difference: about 2 seconds. (from 11:30:14 to 11:30:16). But Time.time is only increased by 0.3333.
What's going on? Is the time lost by instantiation of new objects not included in the deltaTime? Or maybe the Physics calculations? There are physics included in the newly spawned objects.
Seems to be related to:
http://answers.unity3d.com/questions/52673/timetime-running-slow.html
Answer by VildNinja · Jan 29, 2014 at 11:24 AM
Unity has a max time step. Can be found in the menu: Edit -> Project Settings -> Time
this is to prevent things from going out of hand, if a single frame takes too long. Imagine a zombie following your player, if your game freezes for 2 seconds that could make the zombie jump several meters closer to you (in the case where you use delta time to calculate its movement)
And in my case, the max time step is indeed set to 0.3333. This fixes it. Thanks a lot!