What is LateUpdate in Unity And Why It’s Useful
I’m sure you may have come across LateUpdate at least once when you searched for guides and tutorials on the internet. You probably wondered what’s the difference between it and the 2 more commonly used functions, Update and FixedUpdate.
LateUpdate is one of the main Update events. It is called once per frame after the Update has finished. The most common use of LateUpdate is position tracking, such as the following camera.
Keep on reading as I will be explaining some more about the LateUpdate event function and there will be some examples of its use cases below.
Table of Contents
What Is LateUpdate?
LateUpdate is one of the main Update events.
LateUpdate behaves the same way as Update, in which it is always called once per frame.
The only difference is: It is called after the Update function has finished and all of the internal animation updates have been completed.
As you can see from the image above, the LateUpdate function is always called after the Update function and after the internal animation update has already finished.
The LateUpdate function behaves the exact same way as the normal Update would, aside from being called later than it.
Take a look at this:
private void Update() {
Debug.Log("Time.deltaTime inside Update: " + Time.deltaTime);
}
private void LateUpdate() {
Debug.Log("Time.deltaTime inside LateUpdate: " + Time.deltaTime);
}
Code language: C# (cs)
Time.deltaTime yields the same value when used both inside the LateUpdate function and the Update function.
This means Time.time also yields the same value inside both of these functions.
private void Update() {
Debug.Log("Time.time inside Update: " + Time.time);
}
private void LateUpdate() {
Debug.Log("Time.time inside LateUpdate: " + Time.time);
Debug.Log("-----------------------------------------");
}
Code language: C# (cs)
As you can see here, Time.time always gives the same value:
Another thing to note is: The LateUpdate function is also called even when Time.timeScale is set to 0 (zero), just like Update.
private void Start() {
Time.timeScale = 0; // Set the time scale to 0
}
private void Update() {
Debug.Log("Time.deltaTime inside Update: " + Time.deltaTime);
}
private void LateUpdate() {
Debug.Log("Time.deltaTime inside LateUpdate: " + Time.deltaTime);
}
Code language: C# (cs)
The LateUpdate function will still be called even when the time scale is set to 0.
But of course, Time.deltaTime will always be 0 when the time stops.
You can pretty much treat the LateUpdate function as a second Update function. There’s a common use for it. I’ll get into this later in this article.
Is LateUpdate called after FixedUpdate?
You are probably wondering: “What about FixedUpdate? Is LateUpdate also called after FixedUpdate?”
The FixedUpdate function is called once every fixed duration, meaning it can be called once, more than once, or none at all.
This brings up the question:
“What if FixedUpdate is called multiple times in a frame? Can some of the calls happen AFTER LateUpdate?”
The definition of FixedUpdate is a little bit vague when it comes to the order of execution other than “it can be called more than once per frame.”
Looking at the image above, it is implied that the FixedUpdate function is given priority in a frame where it needs to be called.
But is it actually like that?
I have conducted an experiment.
private void Start() {
// Set the frame rate to very low so
// FixedUpdate has a chance of being
// called more than once per frame
Application.targetFrameRate = 10;
}
private void Update() {
Debug.Log("Update is called at: " + Time.time);
}
private void LateUpdate() {
Debug.Log("LateUpdate is called at: " + Time.time);
Debug.Log("-------------------------------------");
}
private void FixedUpdate() {
Debug.Log("FixedUpdate is called at: " + Time.time);
}
Code language: C# (cs)
I limited the frame rate to a very low number so the FixedUpdate function had a chance to be called more than once per frame:
Application.targetFrameRate = 10;
Code language: C# (cs)
After running the game, this was the result:
And it was the same every time, in which the FixedUpdate function, in a span of one frame, was always called before both Update and LateUpdate.
But that’s on a per-frame basis.
The LateUpdate function is always the last “Update” function to be called in a frame. But the FixedUpdate function is on a separate execution flow as it doesn’t depend on the frame rate but rather its own timer, therefore it isn’t necessarily called before or after the LateUpdate function.
One thing we can say for sure is that the LateUpdate function always comes after the Update function.
What Is LateUpdate Used For?
The LateUpdate function is commonly used for forcing script execution order or for position tracking such as making the game camera follow an object.
This is because the LateUpdate function always happens after everything in a frame will have finished its execution, meaning an object will have already moved to a new position and all calculations will have settled, making it more accurate to do position tracking inside LateUpdate.
Sounds confusing, right?
You’re probably thinking: “Why can’t I just put everything inside Update? I can just put the camera code after everything else in there.”
No, we’re talking about 2 things here: an object that moves around and a camera object that follows it.
Let’s say an object has a movement script attached to it. All of its movement is done inside its Update function.
And here we have a camera object that has a script that orders it to follow the aforementioned object, inside the camera’s own Update function.
What do you think is going to happen?
There is no way to control the order of execution of the Update function of 2 separate objects. The Update function of the camera may be called before OR after the moving object’s Update function.
This can cause visual glitches as the camera’s script may have already been executed before the movement of the object even happens. Therefore, it is better and safer to use the LateUpdate function in this situation.
Conclusion
So to conclude, the LateUpdate function behaves exactly like the normal Update function. It can be treated as another Update function that is called later. It is commonly used for anything that needs to wait for other operations to be done inside the Update function first such as the following camera for third-person games.
I hope this has been a good read for you.
Cheers!