Time
TypeClass (static members)
NamespaceUnityEngine

Time

The Time class tells you how much time has passed: since the last frame, since the game started, and at what speed the game is running. It's the key to making things move at the same speed on every computer.

Frames are like the steps you take while walking. A fast computer takes many small steps per second; a slow one takes fewer, bigger steps. Time.deltaTime is the length of your last step in seconds. Multiply your speed by it and you cover the same distance per second no matter how many steps you take.

Static Properties

NameTypeDescription
deltaTimefloatSeconds since the last frame (e.g. 0.016 at 60 FPS). Multiply movement by this. Inside FixedUpdate it returns fixedDeltaTime.
fixedDeltaTimefloatSeconds between physics steps. Default 0.02 (50 per second).
timefloatSeconds since the game started (scaled by timeScale). Good for cooldowns.
timeScalefloatGame speed. 1 = normal, 0.5 = slow motion, 0 = paused.
unscaledDeltaTimefloatLike deltaTime but ignores timeScale. Use for pause menus and UI.
unscaledTimefloatLike time but ignores timeScale.
realtimeSinceStartupfloatReal seconds since the app launched. Keeps counting even when paused.
frameCountintHow many frames have been started.

Code Samples

Frame-rate independent movement

// BAD: moves 0.1 units per frame. 60 FPS = 6 units/s, 144 FPS = 14.4 units/s.
transform.position += Vector3.forward * 0.1f;

// GOOD: moves 5 units per second on every machine.
transform.position += Vector3.forward * 5f * Time.deltaTime;

A cooldown timer

using UnityEngine;

public class Dash : MonoBehaviour
{
    [SerializeField] private float cooldown = 2f;
    private float readyAt;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift) && Time.time >= readyAt)
        {
            Debug.Log("Dash!");
            readyAt = Time.time + cooldown;
        }
    }
}

Pause and slow motion

public void Pause()      { Time.timeScale = 0f; }
public void Resume()     { Time.timeScale = 1f; }
public void SlowMotion() { Time.timeScale = 0.3f; }

UI that still animates while paused

void Update()
{
    // Spins even when Time.timeScale is 0.
    loadingIcon.Rotate(0f, 0f, -180f * Time.unscaledDeltaTime);
}

What happens at runtime

When you set timeScale = 0:

  1. Time.deltaTime becomes 0, so anything multiplied by it stops moving.
  2. Time.time stops increasing, so cooldowns freeze.
  3. FixedUpdate stops being called, so physics freezes.
  4. Animators (in Normal update mode) freeze. WaitForSeconds never finishes.
  5. Update still runs every frame. Input still works, which is how you can un-pause.

Common Mistakes

See also