Loading lessons...
Time, Frame Rate and Fixed Updates
Time, Frame Rate and Fixed Updates
Understanding how Unity measures and manages time is fundamental to writing frame-rate-independent gameplay code.
Time.deltaTime
Time.deltaTime is the time in seconds that elapsed since the last Update() call. Multiplying movement values by it makes motion frame-rate independent:
void Update()
{
// Without deltaTime: faster on high-end machines
// transform.position += Vector3.forward * 5f;
// With deltaTime: always moves 5 units per second
transform.position += Vector3.forward * 5f * Time.deltaTime;
}
FixedUpdate and Time.fixedDeltaTime
FixedUpdate() is called at a fixed interval, not every rendered frame. This is where physics code should live.
- Time.fixedDeltaTime: The interval between FixedUpdate calls (default: 0.02s = 50 Hz).
- You can change it in Project Settings > Time > Fixed Timestep.
void FixedUpdate()
{
// Apply physics forces here, not in Update()
_rigidbody.AddForce(Vector3.forward * _force);
}
Time.timeScale
Time.timeScale controls how fast game time advances relative to real time:
1.0= normal speed0.5= slow motion (half speed)0.0= paused (FixedUpdate stops, Update still runs)2.0= double speed
// Pause the game
Time.timeScale = 0f;
// Resume
Time.timeScale = 1f;
// Slow-motion effect
Time.timeScale = 0.3f;
Note: Time.unscaledDeltaTime gives the real elapsed time regardless of timeScale. Use it for UI animations and menus that should not be affected by pausing.
Frame Rate Control
// Target 60 FPS
Application.targetFrameRate = 60;
// Disable VSync (required for targetFrameRate to take full effect)
QualitySettings.vSyncCount = 0;
Time.time and Time.realtimeSinceStartup
Time.time: Total game time elapsed since startup (scaled by timeScale).Time.realtimeSinceStartup: Real-world seconds since the application started (unaffected by timeScale). Useful for benchmarking.
TL;DR
- Multiply all movement by
Time.deltaTimeto ensure frame-rate independence. - Physics code belongs in
FixedUpdate(), which runs at a fixed timestep. Time.timeScale = 0pauses physics and scaled time; useunscaledDeltaTimefor UI.Application.targetFrameRatecaps the frame rate; disable vSync to use it.