Menu
CoddyTech

Unscaled Time

Part of the Building Real Games section of Coddy's Unity journey. Lesson 35 of 56.

A paused or slowed game still needs a real clock for some things: a pause menu that animates, a countdown before play goes on, a freeze that must end by itself. Time.unscaledDeltaTime is the real time since the last frame, whatever Time.timeScale is, and Time.unscaledTime is the real time since the game started. This pause menu turns its loading wheel with real time, so the wheel spins while the game is paused:

void Update()
{
    wheel.Rotate(0f, 0f, -180f * Time.unscaledDeltaTime);   // half a turn a second, paused or not
}

Coroutines have the same pair. WaitForSeconds counts game time, so at Time.timeScale = 0 it never ends; WaitForSecondsRealtime counts real seconds. Started when the menu opens, this coroutine blinks the menu's title every half second:

IEnumerator Blink()
{
    while (true)
    {
        title.SetActive(!title.activeSelf);
        yield return new WaitForSecondsRealtime(0.5f);
    }
}

Use the scaled values for gameplay, which should stop when the game stops, and the unscaled ones for what must run while it is paused or slowed: menus, their animations, and timers that end a pause. Invoke has no real time version: it always counts game time.

challenge icon

Challenge

Medium

A hit stop makes a heavy blow feel heavy: the whole game freezes for a moment on impact. The arrows move the dropper and Space drops an anvil from it (Dropper.cs, ready). Write Anvil.cs, which every anvil carries:

  • When the anvil hits anything, freeze the game: Time.timeScale 0.
  • freezeTime real seconds later (0.3 in the Inspector), set it back to 1.

Press Play, drop a few anvils, then Check.

Try it yourself

using System.Collections;
using UnityEngine;

public class Anvil : MonoBehaviour
{
    public float freezeTime = 0.3f;

    // Freeze the game for freezeTime real seconds when the anvil hits something
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Building Real Games