FixedUpdate Method in Unity

Introduction

In this arti­cle we are going to ana­lyze Unity’s FixedUp­date method which allows us to make changes over time in our projects.

A method is a func­tion defined in a script that can be exe­cut­ed and per­forms the task we define inside it. To learn more about meth­ods in pro­gram­ming you can read this arti­cle or watch a video that sum­ma­rizes the infor­ma­tion.

IMPORTANT

Here you have two videos about the FixedUp­date func­tion in Uni­ty. In the video on the left we see a pro­to­type made in Uni­ty to point the dif­fer­ences between the Update and FixedUp­date func­tions in Uni­ty. You can down­load the Uni­ty Pack­age in this arti­cle. In the video from the right we see how to grad­u­al­ly change vari­ables in Uni­ty, mak­ing incre­men­tal changes inside the UPDATE or the FIXEDUPDATE method in Unity.

MY UNITY PLAYLIST WITH SEVERAL VIDEOS THAT YOU MAY FIND USEFUL
👇🏽

FixedUpdate Method in Unity — MonoBehaviours

The Update func­tion is defined in the MonoBe­hav­iour class and will run auto­mat­i­cal­ly in each frame of the game if the MonoBe­hav­iour is active.

By default, the time between con­sec­u­tive FixedUp­date runs is 20 mil­lisec­onds or 0.02 sec­onds. This time can be seen and mod­i­fied in the tab Edit > Project Set­tings > Time — Fixed Timestep.

When we cre­ate a new Script in Uni­ty, by default we’ll get some code already writ­ten. In this code is defined a Pro­gram­ming Class that is called equal to the name that we gave to the Script and that extends or inher­its its behav­ior of MonoBe­hav­iour, this in sim­ple terms means that our Script is in itself a MonoBe­hav­iour or a par­tic­u­lar case of MonoBehaviour.

MonoBe­hav­iours can be added to the GameOb­jects that are in the hier­ar­chy, this can be done from the inspec­tor using the “Add Com­po­nent” but­ton or sim­ply drag­ging the Script to the GameOb­ject inspector.

Execution of the FixedUpdate Function

While the game is run­ning, Uni­ty auto­mat­i­cal­ly takes all the MonoBe­hav­iours on the scene and per­forms the FixedUp­date meth­ods every time the "Fixed Timestep" time is met. So we don't have to exe­cute this method man­u­al­ly, the engine takes care of it.

This means that the FixedUp­date func­tion will run peri­od­i­cal­ly while our game is running.

Regard­less of the FPS (frames per sec­ond) of our game, the FixedUp­date method will run at reg­u­lar inter­vals, 50 times per sec­ond if the Fixed Timestep is set to 0.02 seconds.

Conclusion — FixedUpdate for evenly spaced changes in time

The FixedUp­date method rep­re­sents the dynam­ic part of a game in Uni­ty, when we want to pro­duce changes in time and that these changes are applied at reg­u­lar inter­vals, we resort to the FixedUp­date function.

A typ­i­cal appli­ca­tion of this func­tion is to make the move­ment of objects or some ani­ma­tions that we do in a pro­ce­dur­al way.

When mov­ing objects in FixedUp­date, the speed of the object will be the one we indi­cate. If we move objects in the Update func­tion, when our game runs at more FPS, the object will move faster than when the game runs slower.

It is use­ful to under­stand the order of exe­cu­tion of the Start, Update and FixedUp­date meth­ods since it allows us to iden­ti­fy dif­fer­ent moments in the exe­cu­tion of the game.

Scroll to Top
Secured By miniOrange