Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.
HomeBlogUnityProgramming

Update vs FixedUpdate vs LateUpdate

0 Comments - May 27, 2021
416

A prominent part of the Unity API are the update functions, which are the functions that run continuously.

Unity has three types of update functions: Update, FixedUpdate, and LateUpdate.

Update vs FixedUpdate

The difference between Update and FixedUpdate functions is in how often they run.

Update function runs once per frame while FixedUpdate runs at a constant rate, controlled by the "Fixed Timestamp" value in Project Settings -> Time.

Use Case

Update functions are suitable for programming the game logic, player input, and basically any non-physics calculations.

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Space button has been pressed
        }    
    }

FixedUpdate functions are suitable for physics-based calculations, such as Raycasting, applying forces to Rigidbodies, or any calculations that need to be framerate independent.

    void FixedUpdate()
    {
        //Use Physics Raycast to detect if there any object in front
        RaycastHit hit;
        if(Physics.Raycast(transform.position, transform.forward, out hit, 10))
        {
            Debug.Log("Object '" + hit.transform.name + "' is currently in front of this object.");
        }
    }

Update vs LateUpdate

Update and LateUpdate are identical in terms of run frequency (both run once per frame), but LateUpdate runs after all Update functions.

Use Case

LateUpdate function is commonly used to modify animated model bones (ex. making the player model look up and down) or to implement a smooth camera follow.

    void LateUpdate()
    {
        //Make camera tagged 'MainCamera' look at this object transform
        Camera mainCamera = Camera.main;
        mainCamera.transform.LookAt(transform);
    }

Conclusion

Each update function has its own use case.

Using them in combination makes it possible to implement a wide variety of scenarios.

You Do Not Have To Do Everything Yourself
Search from thousands of ready-to-use Unity assets.
Visit Asset Store