DeltaTime
How to use deltaTime when making your game.
deltaTime is the time in seconds it took to complete the last frame.
- 0.01666 if you are getting 60fps (1 divided by 60)
- 0.03333 if you are getting 30fps
Multiplying by deltaTime will make sure things happen in units per second, instead of units per frame.
If you do not multiply by deltaTime, you will get different values on different devices. On faster machines, objects will move faster and be further ahead of objects on slower machines.
When to Use?
Usually when you have code that needs to do something over time.
- Moving any object over time.
- Incrementing a number over time.
Examples
Moving an object along the X axis
If you have a frame-rate of 60, after 1 second, the cube will be at position (60, 0, 0).
If you have a frame-rate of 30, after 1 second, the cube will be at position (30, 0, 0).
Not good!
After 1 second, the cube will be at position (1, 0, 0), regardless of frame-rate. Everyone gets the same result.
Good!
Notes
- http://docs.unity3d.com/ScriptReference/Time-deltaTime.html
- http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/#post-2442966
- http://fabiensanglard.net/timer_and_framerate/index.php