Course contentsShow
← Unity Game Development
Lesson 119 of 8282. Scripting and LifecycleFree lesson

Implementing Frame-Rate Independent Movement with DeltaTime

Apply the Time.deltaTime multiplier to translation vectors to ensure objects move at a consistent speed regardless of frame rate.

Implementing Frame-Rate Independent Movement with DeltaTime

In this lesson, you will learn how to stabilize your object movement so that it feels the same on every computer, regardless of how fast the game is running.

The Problem with Frame-Dependent Movement

When you write code like transform.Translate(Vector3.forward * speed); inside your Update() method, you are telling the object to move a specific distance every time a frame renders.

The issue is that different computers have different frame rates. A computer running at 120 frames per second (FPS) will execute that line of code twice as often as a computer running at 60 FPS. As a result, your object will appear to move twice as fast on the more powerful machine. This makes for an inconsistent experience that ruins gameplay.

The Solution: Time.deltaTime

To fix this, we multiply our movement vector by Time.deltaTime.

Time.deltaTime represents the time (in seconds) that has passed since the last frame was rendered. By multiplying your movement by this value, you are essentially saying: "Move this many units per second," rather than "Move this many units per frame."

If the frame rate drops, Time.deltaTime gets larger, increasing the movement distance per frame to compensate. If the frame rate is high, Time.deltaTime gets smaller, decreasing the distance per frame. The result is a smooth, consistent speed.

Practical Implementation

Here is how you apply it to your translation logic:

public float speed = 5.0f;

void Update()
{
    // We multiply by Time.deltaTime to ensure consistent speed
    transform.Translate(Vector3.forward * speed * Time.deltaTime);
}

By adding * Time.deltaTime, your object will travel exactly speed units every single second, no matter how many times the Update() method fires. This creates a predictable experience for every player, ensuring that your game mechanics—like character speed or patrol paths—remain balanced across all hardware.

Key Takeaway: Always multiply your movement logic by Time.deltaTime to convert "units per frame" into "units per second," ensuring consistent behavior regardless of fluctuating frame rates.