The Difference Between Update And FixedUpdate In Unity
You may have come across multiple Unity guides or tutorials in your game development journey. As you follow along with them, you often see that a majority of the code is written inside a function called “Update” and sometimes you see it inside a function called “FixedUpdate.” You might be wondering what’s the difference between the two and when to use one over another.
The Update function runs exactly once every frame, whereas the FixedUpdate function runs at a fixed interval independent of your game’s frame rate, making it possible to run once, zero, or multiple times per frame.
In this article, I’ll tell you everything you need to know about the difference between the Update function and the FixedUpdate function, how and when to use them. Make sure you keep reading to learn more!
Table of Contents
- The Main Difference Between Update And FixedUpdate In Unity
- Update VS FixedUpdate
- When Should I Use Update Function In Unity?
- When Should I Use FixedUpdate Function In Unity?
- Can FixedUpdate Rate Be Changed In Unity? If So, How?
- Is Update Faster Than FixedUpdate In Unity?
- How To Get The Interval Between Each Update Call In Unity
The Main Difference Between Update And FixedUpdate In Unity
The Update function runs exactly once per frame, while the FixedUpdate function runs at a fixed rate. What this means is: The Update function will run as often as your game’s frame rate. For example, if your game runs at 120 frames per second, the Update function will be called 120 times within 1 second.
And as for the FixedUpdate function, it is called on a fixed rate. This means, in every frame of your PC’s frame rate, FixedUpdate may be called once, more than once, or not at all as it doesn’t synchronize with the frame rate. By default, it is set to be called every 0.02 seconds.
I’ll put a comparison table below so it’s easier for you to read.
Update VS FixedUpdate
|Frequency of execution||Once per frame||Once every pre-determined seconds|
(default: every 0.02 seconds)
|Related Delta Time Property||Time.deltaTime||Time.fixedDeltaTime|
|Common usage||Pretty much everything||Physics-related operations|
|Consistency||Not consistent, can fluctuate due to lag and different frame rates||Consistent|
(same interval every time)
When Should I Use Update Function In Unity?
The Update function can be used for anything that needs to be executed constantly and automatically without needing to be called manually from outside sources.
A good example would be: Say you want to make a character patrol an area by moving forward a certain distance and then move back to the start once it has reached the destination and do it all over again, then you would write code in the Update function like the following:
You can use the Update function to update the character’s position, check the direction it is moving, and check if it has reached the coordinate where it has to turn around and move back to the start and then start all over again.
By putting this logic inside the Update function, the character will move back and forth automatically as soon as the script starts running without you having to do anything else because the Update function will keep running the same block of code over and over.
The Update function is also good for detecting and capturing button presses as it is more responsive than FixedUpdate because of frame rate synchronization, whereas FixedUpdate may or may not detect button presses if you put button press detection script in there because it doesn’t get called every frame like the Update function.
But then you may have a question like: “But what if I want to press a button to make an object move and collide with another object? Isn’t that related to physics?”
Well, yes, but no, what I’m talking about is: You capture the inputs, save them in variables (moveButtonPressed, for example), and then you refer to those variables inside the FixedUpdate function to deal with the movement and the collision calculations in there.
Another reason for using the Update function over the FixedUpdate function is when you want to control an object without the use of Unity’s built-in physics engine such as moving an object by setting the position of the object directly every frame manually. This can be done from within FixedUpdate too but it will look choppy and stuttery at a high frame rate.
When Should I Use FixedUpdate Function In Unity?
FixedUpdate is generally preferred over the Update when it comes to dealing with logic that is related to physics calculations. This is because the physics engine also runs on the same interval as the FixedUpdate function at the same fixed rate.
This means physics-related functions applied within the FixedUpdate function are sure to get calculated properly and promptly unlike in the regular Update function.
For this reason, we don’t use the regular Update function for physics-related tasks as it can get called multiple times before physics calculations are executed when the game is on a high frame rate and the reverse is also true, the physics calculations may happen more than once per frame when the game runs on a low frame rate, thus creating inconsistency across different systems and increasing the chance of bugs and issues appearing.
One of the most common functions to use inside the FixedUpdate function is RigidBody as it controls an object through physics simulation such as adding force, adding torque, and changing velocity.
Can FixedUpdate Rate Be Changed In Unity? If So, How?
FixedUpdate‘s rate of execution can be changed from within Unity. To change the FixedUpdate rate value, go to Edit > Settings > Time > Fixed Timestep. By default, the value is set to 0.02 seconds which is 50 times per second, and it will be called constantly at this interval, independent of frame rate.
Is Update Faster Than FixedUpdate In Unity?
The Update function is and isn’t faster than FixedUpdate. Depending on the frame rate of your game, the number of times the Update function is called can change, making it faster or slower than the FixedUpdate rate.
For example, let’s say the FixedUpdate rate is at the default value which is 0.02-second interval or 50 times per second and your game runs at 100 frames per second, making the Update function runs 100 times per second. This results in the Update function being called twice as often as the FixedUpdate. Therefore, Update is faster than FixedUpdate.
On the other hand, if your game runs at the rate of 25 frames per second and the FixedUpdate rate is at the default value, this will make the Update function runs half as often as the FixedUpdate, which is slower.
How To Get The Interval Between Each Update Call In Unity
To find out how long has passed since the last Update call, simply run Time.deltaTime, or Time.fixedDeltaTime for FixedUpdate. This will return the amount of time that has passed since the last Update call in seconds. For FixedUpdate, it will simply return the value of Fixed Timestep that you have configured (the default is 0.02 seconds).
For Update, this value is not consistent and can be affected by lag and stuttering.
There are also Time.unscaledDeltaTime and Time.fixedUnscaledDeltaTime which are similar to the scaled version mentioned above, except these ones are unaffected by the global time scale. This means you can pause the game and still have something moving.
For other Time-related properties, please refer to Unity documentation on Time class.
FixedUpdate is preferred over Update in the scenarios where you need to deal with anything physics-related as it has a fixed execution rate (every 0.02 seconds as the default) and it is always aligned with the physics engine. For everything else, Update is your best friend. Thank you for reading and I hope you learned something new today!