0
0
Unityframework~5 mins

FixedUpdate vs Update in Unity

Choose your learning style9 modes available
Introduction

FixedUpdate and Update are two ways Unity runs code repeatedly. They help control when and how often your game actions happen.

Use Update to check for player input like pressing keys or clicking the mouse.
Use FixedUpdate to move objects smoothly when physics like gravity or collisions are involved.
Use Update to update animations or UI elements every frame.
Use FixedUpdate to apply forces or change velocity on objects with Rigidbody components.
Use Update for things that need to happen every frame, and FixedUpdate for physics calculations.
Syntax
Unity
void Update() {
    // code runs every frame
}

void FixedUpdate() {
    // code runs at fixed time intervals
}

Update runs once every frame, which can vary in time depending on the device.

FixedUpdate runs at a steady, fixed time step, good for physics calculations.

Examples
This checks for a spacebar press every frame.
Unity
void Update() {
    if (Input.GetKeyDown(KeyCode.Space)) {
        Debug.Log("Jump pressed");
    }
}
This applies an upward force at fixed intervals for smooth physics movement.
Unity
void FixedUpdate() {
    Rigidbody rb = GetComponent<Rigidbody>();
    rb.AddForce(Vector3.up * 10);
}
Sample Program

This script moves an object forward smoothly using FixedUpdate and logs when the W key is pressed using Update.

Unity
using UnityEngine;

public class MoveObject : MonoBehaviour {
    Rigidbody rb;

    void Start() {
        rb = GetComponent<Rigidbody>();
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.W)) {
            Debug.Log("W key pressed");
        }
    }

    void FixedUpdate() {
        rb.MovePosition(rb.position + Vector3.forward * 0.1f);
    }
}
OutputSuccess
Important Notes

Update frequency depends on frame rate, so it can be uneven.

FixedUpdate runs at consistent intervals, making it better for physics.

Never put physics code in Update to avoid jittery movement.

Summary

Update is for frame-based actions like input and animations.

FixedUpdate is for physics and smooth movement at fixed time steps.

Use each one where it fits best to keep your game running smoothly.