What is LateUpdate in Unity: Explanation and Usage
LateUpdate is a Unity method called once per frame after all Update methods have run. It is used to perform actions that depend on other objects' updates, such as camera movement following a character.How It Works
In Unity, the game runs in frames, and each frame updates the game state. The Update method is called first on all scripts to handle things like player input or movement. After all Update calls finish, Unity calls LateUpdate on all scripts.
Think of it like a relay race: Update runners pass the baton first, and then LateUpdate runners take it to finish the lap. This order ensures that LateUpdate can react to everything that changed during Update.
This is useful when you want to adjust something after all other updates, like making a camera follow a player smoothly after the player has moved.
Example
This example shows a simple camera script that follows a player object. The camera moves in LateUpdate to ensure it updates after the player has moved in Update.
using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform player; private Vector3 offset; void Start() { offset = transform.position - player.position; } void LateUpdate() { transform.position = player.position + offset; } }
When to Use
Use LateUpdate when you need to perform actions after all Update methods have run in a frame. Common cases include:
- Camera follow scripts that track moving objects
- Adjusting animations or physics after movement
- Synchronizing objects that depend on others' updated positions
This helps avoid jitter or incorrect positioning caused by updating too early.
Key Points
LateUpdateruns once per frame after allUpdatecalls.- It is ideal for camera and follow logic.
- Helps ensure smooth and correct positioning after all updates.
- Does not replace
Update, but complements it.
Key Takeaways
LateUpdate runs after all Update calls each frame to finalize changes.LateUpdate for camera follow and dependent object adjustments.Update.LateUpdate; keep those in Update or FixedUpdate.