script putting movement under "void FixedUpdate" vs putting movement in its own "void XXXX"?

Hi everyone,

I was just wondering what the difference is between these two.

The first tutorial video i followed had in the script all the movement related code under “void FixedUpdate()”. There was also some movement code under a custom “void Flip()” thats purpose was to just flip the character left/right depending on conditions.

I just saw a different tutorial by CodeMonkey where the movement code was in its own void called “void HandleMovement()”. The things i noticed about his script is that #1 i do not see any “void FixedUpdate()” anywhere and #2 i see “HandleMovement();” under “void Update()”.

This makes me think that void Update is “calling” or “invoking” i think the term is, the void HandleMovement…is that correct? so does that means that HandleMovement since it was called under void update executes every frame? but if instead HandleMovement was called under void fixedupdate then it would update at the rate specified by the editor?

So what are all the advantages of making the movement its own void? as opposed to just typing all the movement code under update or fixed update?

Hi! Both FixedUpdate() and Update() are called regularly, but a bit differently: while Update() is called every frame, FixedUpdate() has the frequency of the physics system; it is called every fixed frame-rate frame. (Source: Unity - Scripting API: MonoBehaviour.FixedUpdate())

It would be good to implement physics etc in FixedUpdate to make it less dependent on the graphics rendering speed. In practice, these things are often implemented in Update() but then made dependent on the time that passed using Time.deltaTime.

Also see here: Update and FixedUpdate - Unity Learn

1 Like

As pointed out by @influjensbahr , you normally want to run your “physics simulation” (for example changing the speed of your entities) in FixedUpdate. The way I see it there are 2 reasons for this:

  • Since your computations always receive the same time difference between executions, the result will be much more predictable (operations with floats are inaccurate and the error adds up with every consecutive operation).
  • If you need to do any collision check you’ll have much fresher information (since it’s executed right before updating the physics world state, which normally is done multiple time between every Update execution).

I’d recommend using Update for stuff that only needs to be checked once per frame (time counters, caching controller state, etc.)

As for the reason to put all the code in a dedicated method rather than directly in Update/FixedUpdate, it all boils down to a matter of encapsulation. When your code begins to grow and you start doing many different things in those method, you’ll ideally want to have every functionality neatly separated on their own method (or classes, if they become complex enough).

It wasn’t really the difference between update and fixedupdate that i was asking about, i have a bit of understanding what those are for.

In the video link below at time stamp 5:48 Code Monkey puts his jump code under void update which seems normal to me, my jump code is under void fixedupdate because on a different tutorial that i followed thats where the instructor put it. At 17:47 he adds his movement code and this is what i was asking about. His movement code is called void handlemovement and it is its own void. I do see that the void handlemovement is typed out (called/invoked?) in the void update. So that makes me think that since it was called in the void update then it update every frame. But if instead it had been called under void fixedupdate it would be updating at the physics system rate.

I was guessing that it was to make things neater like you mentioned DrDemencio but i was wondering if there were other reasons for wanting to do it this way or maybe reasons that you might not want to do it this way. I started a test project specifically to try out Code Monkey’s way of neatly organizing voids.

I see. That way of writing the code into a separate method is usually done to make the code easier to read. It could also be a good idea to do so if you ever want to call the functionality from somewhere else as well, so it can be used from more places than the Update() functions.

2 Likes