The difference between Update, FixedUpdate and LateUpdate
The difference between Update, FixedUpdate and LateUpdate generally create confusion in beginner game developers’ mind. In this post, we will examine what they are, when and why they are used.
Behind the scenes, some pieces of code are executed before the scene is loaded, when started or finished. In addition to this, some pieces of code are executed periodically. We use Update(), FixedUpdate() and LateUpdate() methods to perform this periodic tasks.
Update() Method
Update() is executed every frame. For instance, if the framerate is 60, then the Update method runs 60 times in a second. When we want to control or calculate something which is not related to physics, we use Update(). Since the framerate can be changed due to several reasons, using Update() method in physics calculations may give you unexpected results.
FixedUpdate() Method
FixedUpdate() runs in fixed time steps. Therefore we use this method when a task or a calculation is time-dependent. For example, physics calculations are framerate independent. We have to find the same results even if the framerate changes. Therefore, we write our physics-related code in FixedUpdate() method.
As we mentioned above, this method is executed in fixed time steps. The default time-step is 0.02 seconds. This means that FixedUpdate runs 50 times in a second regardless of the framerate in default options. Nonetheless, this time step can be changed from Edit > Settings > Time > Fixed Timestep. But you should be careful if you already wrote some physics code. You may need to modify the code which you wrote before.
In code, you can access the fixed time step value using Time.fixedDeltaTime.
LateUpdate() Method
LateUpdate() method is also called every frame but after Update() method is executed. Therefore, if you want to check and control something after Update() method finishes its job, then you need to use LateUpdate() method.
Execution Order
As indicated in the Unity Documentation, when a scene is started, physics and physics-related calculations are done first. This means that, FixedUpdate() method is executed before others, since we do physics calculations in this event function.
After FixedUpdate(), Update() method is called. And finally, LateUpdate() is executed.