Hi there. While navigating my terrain I get a sudden jump in the Time.deltaTime value. When this happens the screen “freezes” for a very brief time (maybe 0.25 seconds). This happens on average every 6-7 seconds. Does anyone know what is happening?
Does it also happen if you don’t move or rotate the camera too much? Otherwise it might be the terrain LOD system working, but 0.25 seconds sounds too high for that unless it’s a really complex terrain or a slow computer.
Keep an eye on a CPU monitor while you’re playing. See if Unity (or anything else) is spiking the CPU at that moment.
If Unity is, try disabling various components until the freeze stops happening. It sounds like the terrain is a likely culprit, but not necessarily.
This spike happens regardless of weather the camera is moving or not moving. Also, it happens inside both Windows and Mac OS X.
I was looking at the CPU usage and there are no spikes whatsoever. Additionally I have even deactivated the Terrain but this has not resolved the problem.
Do your scripts allocate lots of objects at runtime? You might be experiencing garbage collection kicking in.
I’ve had similar problems as well, so I built a stripped down test project to see if I could figure out what the problems were. As Aras says, I was afraid of it being the GC kicking in (I’m instantiating objects quite often in the project where the problem is most noticable). However, in this project, the garbage creation should be at a bare minimum, and I still get these deltaTime spikes (not as often though).
The only garbage I think is generated in this test project is string creation (for deltaTime stats) and probably the update of the vertex buffer used for the deltaTime graph. Both of these can be individually turned off using the toggle buttons on the screen.
I built two web-players that illustrate the problem (with and w/o vsync, not sure if that has any effect in a web player):
EDIT: Running in full-screen makes the graph go crazy on my system(s).
EDIT2: Giving input by pressing a key or a mouse button in the game window also makes the frametime jump!
http://www.terravision.no/~trondabu/frametimetest.html
http://www.terravision.no/~trondabu/frametimetest_novsync.html
I also packed down the scene in a package and attached it (in case any of you would want to see what it actually contains).
Any input on this matter is highly appreciated!
Thanks ToreTank, this demo is very illustrative.
I get the problem with spikes in deltaTime in most of my projects as well (maybe all? I’m not sure) and these projects have very little in common. I think it’s a serious problem (it sometimes screws up my simulations severely) but I have no idea what to do about it.
I can confirm with your web-players that:
-
There are tall spikes in the deltaTime every couple of seconds.
-
Clicking the mouse creates large variances in the deltaTime (except when in full-screen). It actually makes the deltaTime significantly lower. Just moving the mouse has the same effect here.
-
Running in full-screen creates lots of small spikes (more like a consistent zig-zag curve).
-
The above applies both when v-synch is on and off. However, the disturbances when clicking or moving the mouse are larger when v-synch is off.
If this is not a bug in Unity or something else that can be fixed in the engine, then I think some tutorials on how to minimize the problem are in order, other than just “try not to generate objects at runtime”.
Rune
You are right about getting lower frametimes when giving input, I get that too (in retrospect, I see that “jump” might not have been the best description).
In case you are not aware - there is a Time.smoothedDeltaTime variable which will smooth out the spikes - while this will only help the visual problems to a certain degree (it does get better, at least some), it might prevent your simulations from exploding (In the following testcase, the lower part of the graph is using the smoothedDeltaTime-variable).
http://www.terravision.no/~trondabu/frametimetest_smoothed.html
I too would very much like to know if these spikes can be prevented somehow, or what causes them.