I’ve lately come across a few posts and questions about FixedUpdate
, more specifically about using FixedUpdate()
at very high frequency to have a more responsive game loop / time things more precisely.
This results from a misunderstanding of the docs, which are somewhat ambiguous:
MonoBehaviour.FixedUpdate()
Description
This function is called every fixed framerate frame
The truth is that FixedUpdate
does not run at it’s own frequency. It is simply updated many times if needed right before Update
, right after Time.fixedTime
is, letting code in FixedUpdate
pretend it runs at that fixed frequency.
Simply run the following code at a very low fixed time step to see what I mean:
void FixedUpdate()
{
Debug.Log( "FixedUpdate realTime: "+Time.realtimeSinceStartup);
}
void Update()
{
Debug.Log( "Update realTime: "+Time.realtimeSinceStartup );
}
You’ll notice FixedUpdate
being called a bunch of times in a row right before Update
, and then a gap.
Except for the audio thread, everything in unity is done in the main thread if you don’t explicitly start a thread of your own. FixedUpdate
runs on the very same thread, at the same interval as Update
, except it makes up for lost time and simulates a fixed time step.
Bottom line: using FixedUpdate
for anything else than physics is in the vast majority of cases simply misguided. If you need to call a function a bunch of times because it’s late to the party, call it a bunch of times from Update
or a Coroutine
, don’t let fixedTime
fool you into believing it’s called at fixed time steps. Plus increasing fixedStep
will increase the cpu load that all your physics code generates, potentially nastily decreasing performance.
The docs for execution order of event functions sum it up nicely:
So in conclusion, this is the execution order for any given script:
All Awake calls
All Start Calls
while (stepping towards variable delta time)
All FixedUpdate functions
Physics simulation
OnEnter/Exit/Stay trigger functions
OnEnter/Exit/Stay collision functions
Rigidbody interpolation applies transform.position and rotation
OnMouseDown/OnMouseUp etc. events
All Update functions