In this article we analyze an experiment to understand what are the differences between Update and FixedUpdate in Unity. To see these differences I set up a simple Unity scene with two cubes and some programming scripts, you will find the Unity Packge for download in this article.
Download Unity Package
Below you can download a Unity package that you can import in your Unity project and see how two objects behave with a translation applied in the Update method in one case and in the FixedUpdate method in the other.
In the following video we see a prototype made in Unity to point the differences between the Update and FixedUpdate functions in Unity.
What are the Update and FixedUpdate methods
To begin with, the Update and FixedUpdate methods are functions that belong to the MonoBehaviour class and will be executed automatically and cyclically when we place those Scripts in the hierarchy.
Summary of the differences between Update and FixedUpdate in Unity
1. The amount of Update executions per second is variable, while the amount of FixedUpdate executions per second is fixed.
2. In general, you have no control over the execution of the update method, it will depend on the performance of the computer in which the game is running. For the FixedUpdate function we can change the Fixed Time Step, by default is 0.02 seconds (20 milliseconds), so we know exactly how many times this function is executed.
3. Both functions are used to make changes in time, from the above it can be concluded that the Update method is more convenient for the logical part and FixedUpdate for the physical part, movements and animations, i.e. actions that must change regularly in time.
How to see the differences between Update and FixedUpdate using the package to download
When you import the package you will have a folder that contains all the necessary files, what you have to do is open the scene that is inside the folder, called “UpdateFixedUpdate”.
Inside the scene we have a frame per second counter to know the refresh rate of the game at all times.
We have two Cubes called Update and FixedUpdate, both have assigned a script called “UpdateFixedUpdate” in which is defined a speed for the cube movement and a logical variable that will determine if the cube movement is done in the Update function or in the FixedUpdate function.
Then there is another GameObject called Saturation that has assigned a Script called “CreateObjectsForever”, when this Script is activated it will start instantiating cubes as children of the Saturation GameObject in an uninterrupted way, which will cause little by little a processing overload and with a little analysis it will allow us to understand the differences between Update and FixedUpdate in Unity.
Procedure for Analysis
First Test without Overload
Initially we make sure that the GameObject Saturation is disabled or your “CreateObjectsForever” script is disabled, so that initially no saturation occurs in the game.
We make sure that both objects have the same speed in their Script UpdateFixedUpdate and that in the Update object the box “Use Fixed Update” is unchecked, while in the FixedUpdate object, that box is checked.
We enter the game mode and observe how the cubes behave. In my case we can see that the cube that moves using the Update method moves faster than the cube that moves in the FixedUpdate method. The cube that moves with Update does so at more than twice the speed and sometimes moves faster and sometimes slower, while the other cube moves regularly over time.
Second Test with Overload
Now we activate the Saturation object (or its script), to activate the instantiation of the objects.
When entering the Game Mode, you will notice that the game’s FPS begin to gradually decrease. This change can be even more drastic if we select the Saturation object in the hierarchy and if we are looking at it closely in the Editor tab. But the most important thing that we notice is that the speed of the cube that moves with Update starts to decrease, the more the overload, the slower the cube moves, until in a moment it is overtaken by the cube that moves with FixedUpdate.
This indicates that the higher the processing saturation, the Update function is executed less times per second, while the FixedUpdate function is executed regularly in time and, even though the game starts to have lag, the movements will be proportional.