When you play a game, you want it to be responsive. Additionally, you want the game to respond similarly regardless of the hardware you are using. A phone and a desktop computer often have very different processors. A modern gaming machine will have a high-speed processor that can crank out a lot of power. Older systems tend to operate more slowly when playing games. Regardless of the machine, you would like to see similar action on the screen when you play. This is most important when you are in a multi-player game. You would not want the person with the faster machine to have an advantage!
Computer displays operate by showing frames like a movie. This creates the animation and the overall action in the game. On faster machines, more frames can be displayed in any given second. Slower machines tend to be able to display fewer frames per second.
With a game, if you move a character one unit for each frame, on a machine that can display 100 frames per second, you’d move 100 units. On a machine that displays 10 frames per second, you’d only move 10 units. This is illustrated in Figure 1.
Figure 1: Performance differences between two machines
If these two machines were being used to play a game against each other, the faster machine would display ten moves for every one move on the slower machine. If the two players were racing to a location in the game, the fast machine would invariably win. That’s not good and would quickly become a bad experience for the person on the slow machine. As the Figure 2 shows, going 50 units at a speed of 1 unit per frame would take the slow machine 5 seconds whereas the fast machine could get there in just a half second.
Figure 2: Making a Game Fair by Using Time.deltaTime
In Unity, the movement of your character or game object is likely to be contained in code controlled within the Update method. To help normalize the speed of movement across systems that might vary in speed, you can use the Unity function Time.deltaTime. deltaTime is the time between the previous frames. Although this is not a constant value, it can be used to help normalize what is displayed. When added to movement calculations in your code, it can help two machines offer similar movement experience in the game even if they are operating a completely different frame rates.
On the slower machine mentioned above, movement would happen 10 times every second, or at every one tenth (.1) seconds. Thus, the deltaTime would be roughly .1 seconds. On the faster machine, each movement would happen every hundredth of a second, so the deltaTime would be .01 seconds.
By multiplying the deltaTime value against the movement distance or speed, you can get to a more consistent movement. Your speed calculations would be replaced with:
speed * Time.deltaTime
In the above illustration of moving 50 units, if we wanted a player to arrive in one second, the speed could be set to 50 units per second. As a result, with this speed of 50 units, the calculations for the two machines listed earlier would be 50 * Time.deltaTime, which would be:
Slow Machine: ( 50 * .1 ) units per frame, or 5
Fast Machine: ( 50 * .01 ) units per frame, or .5
For each frame, the faster machine would move a shorter distance. Because it does more frames, the distance moved over time would be equal regardless of the machine. In one second, the slow machine would move 10 * 5, or 50 units. On the faster machine, which does 100 frames per second, the distance moved in one second would be 100 * .5, or also 50 units! See the comparison in Figure 3.
Figure 3: Frames processed per second on two machines
Summing Up Time.deltaTime
In short, by using the Time.deltaTime method and multiplying it by your speed, you help create a constant movement. You get a change in movement value by second instead of by frame. More importantly, you get a multi-user or cross-device experience that is consistent and fair!