Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

The Game Loop: How Unity Runs Your Game

Understand the game loop behind every Unity game: frames, delta time, Update versus FixedUpdate, the order of events in one frame, and a runnable C# loop you can experiment with.

Every game, in every engine, is a loop. Read input, update the world, draw the picture, repeat. If the loop runs 60 times per second, the player sees 60 frames per second. Understanding this loop is the single most useful thing you can learn early, because nearly every beginner bug ("my character is faster on my friend's computer", "my physics jitters", "my object moves only when I click") is a misunderstanding of how often your code runs.

Unity owns the loop. You never write while (true) yourself; instead you write methods, such as Update, and Unity calls them at the right moment of every frame. This lesson shows what the loop contains, the difference between Update and FixedUpdate, and a runnable simulation of the loop in plain C#.

One frame in a nutshell

At a high level Unity does this each frame:

  1. Process input (keyboard, mouse, touch, gamepad).
  2. Run script Update methods on every active, enabled component.
  3. Run animation and coroutines that were waiting for this frame.
  4. Run LateUpdate methods (for things that must happen after everything moved, like a follow camera).
  5. Render the scene to the screen.

Physics runs on its own clock. Before Update, Unity may run zero, one or several physics steps, each calling FixedUpdate on your scripts, until the physics clock has caught up with real time.

flowchart TD
    A["Start of frame"] --> B["Input events"]
    B --> C{"Physics clock behind?"}
    C -- yes --> D["FixedUpdate, then physics step"]
    D --> C
    C -- no --> E["Update"]
    E --> F["Animation / coroutines"]
    F --> G["LateUpdate"]
    G --> H["Render"]
    H --> A

The physics loop can run several times inside one frame, or not at all.

Frame rate and delta time

A frame does not take a fixed amount of time. On a fast PC it may take 4 ms (250 FPS), on a phone 33 ms (30 FPS), and a heavy scene on any device can have a frame that takes 100 ms. The time between the start of the previous frame and the start of this one is called delta time, available as Time.deltaTime in seconds.

If you write position.x += 0.1f; in Update, the object moves 0.1 units per frame. At 60 FPS that is 6 units per second; at 30 FPS only 3. Multiply by delta time to get the same speed on every machine:

C#
// read-only Unity script
using UnityEngine;

public class Walker : MonoBehaviour
{
    public float speed = 3f;   // units per second

    void Update()
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
    }
}

Chapter 3 returns to this in Time and Frame-Independent Movement. For now, accept the rule: per-frame values need * Time.deltaTime; per-second values do not.

Update vs FixedUpdate vs LateUpdate

Method When it runs Use it for
Update Once per rendered frame, variable interval Input, game logic, movement of non-physics objects
FixedUpdate At a fixed interval (0.02 s by default), possibly several times per frame Applying forces to a Rigidbody, any physics code
LateUpdate Once per frame, after every Update Cameras that follow, final adjustments

Why a separate fixed step? Physics simulation is only stable and repeatable when time advances in equal steps. Variable steps give jitter and make results depend on the frame rate. So Unity decouples the two: physics always advances in 0.02 s pieces, whatever the frame rate.

A runnable game loop

The following program is the same idea as Unity's loop, written in plain C#. It simulates frames of varying length (a fast frame, a slow frame, a very slow one) and shows how the fixed-step accumulator decides how many physics steps run in each frame. Run it and read the output.

C# Variable frames, fixed physics steps (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

A 120 ms frame produced six physics steps at once; a short 8 ms frame produced none. Unity also caps the work with Maximum Allowed Timestep (0.333 s by default), so a long hitch cannot trigger hundreds of steps and freeze the game further.

Why * deltaTime matters: a comparison

This second stand-in moves an object in two ways for one simulated second at two frame rates. One way forgets delta time; the other uses it.

C# Per-frame vs per-second movement (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

The per-frame version covers 3, 6 or 14.4 units in the same second, which is the "my game is faster on my friend's computer" bug. The deltaTime version always gives 6.0.

Other timing values you will meet

  • Time.time: seconds since the game started (scaled by time scale).
  • Time.unscaledDeltaTime: delta time that ignores Time.timeScale, useful for pause menus.
  • Time.fixedDeltaTime: the physics step size (0.02 by default; change it in Edit > Project Settings > Time).
  • Time.timeScale: multiplies game time. 0 pauses Update-time movement and physics; 0.5 is slow motion.
  • Application.targetFrameRate: ask Unity to aim for a frame rate (useful on mobile, where 30 or 60 saves battery).
  • VSync: locks frames to the monitor refresh rate, so you usually see 60 or 144 FPS even with an empty scene.

Common mistakes

  • Putting physics code (forces, Rigidbody.velocity) in Update; it should be in FixedUpdate.
  • Reading input in FixedUpdate; key presses can be missed because FixedUpdate may not run in a frame. Read input in Update, store it, use it in FixedUpdate.
  • Moving a Rigidbody object by setting transform.position in Update; it fights the physics system.
  • Forgetting Time.deltaTime, and then tuning speed numbers on one machine.
  • Heavy work in Update, such as searching the whole scene every frame. See optimization for what to avoid later.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Time.deltaTime measure?

    deltaTime is the duration of the last frame in seconds, so speed multiplied by deltaTime gives distance per frame.
  2. 2. Where should you apply forces to a Rigidbody?

    FixedUpdate runs in step with the physics system, so forces are applied consistently.
  3. 3. How many times can FixedUpdate run in a single frame?

    Unity runs as many fixed steps as are needed to catch the physics clock up with real time.
  4. 4. What does Time.timeScale = 0 do?

    With a timeScale of 0, Time.deltaTime is 0 and FixedUpdate stops running, so the game is effectively paused.

Summary

  • A game is a loop: input, update, physics, render, repeat; Unity owns it and calls your methods.
  • Frame times vary; multiply per-second quantities by Time.deltaTime.
  • Update runs once per frame, FixedUpdate at a fixed 0.02 s interval (zero or more times per frame), LateUpdate after every Update.
  • Read input in Update, apply physics in FixedUpdate.
  • Time.timeScale slows or pauses the game; unscaledDeltaTime ignores it.

Next: A Tour of the Editor Windows