James Frowen

A collection of game development stuff.



Back to list of tips

LateFixedUpdate

A version of FixedUpdate that will run after all others

public void OnEnable()
{
    StartCoroutine("RunLateFixedUpdate");
}
public void OnDisable()
{
    StopCoroutine("RunLateFixedUpdate");
}
public IEnumerator RunLateFixedUpdate()
{
    while (true)
    {
        LateFixedUpdate();
        yield return new WaitForFixedUpdate();
    }
}

The Unity documentation on Execution Order shows that WaitForFixedUpdate is run after the Internal physics update. This mean you can use LateFixedUpdate in order to check the physics state of the game before the next FixedUpdate.

LateFixedUpdate Logs

The following set up can be used to test this

public class Script1: MonoBehaviour
{
    public void OnEnable()
    {
        StartCoroutine(RunLateFixedUpdate());
    }
    public void OnDisable()
    {
        StopAllCoroutines();
    }
    public IEnumerator RunLateFixedUpdate()
    {
        while (true)
        {
            LateFixedUpdate();
            yield return new WaitForFixedUpdate();
        }
    }

    public void FixedUpdate()
    {
        Debug.Log("Script1 FixedUpdate:" + Time.time);
    }
    public void LateFixedUpdate()
    {
        Debug.Log("Script1 LateFixedUpdate:" + Time.time);
    }
}
public class Script2: MonoBehaviour
{
    public void OnEnable()
    {
        StartCoroutine(RunLateFixedUpdate());
    }
    public void OnDisable()
    {
        StopAllCoroutines();
    }
    public IEnumerator RunLateFixedUpdate()
    {
        while (true)
        {
            LateFixedUpdate();
            yield return new WaitForFixedUpdate();
        }
    }
    
    public void FixedUpdate()
    {
        Debug.Log("Script2 FixedUpdate:" + Time.time);
    }
    public void LateFixedUpdate()
    {
        Debug.Log("Script2 LateFixedUpdate:" + Time.time);
    }
}
Back to list of tips