Fixed update with Physics.Simulate in Unity

When we set ‘Physics.simulationMode’ to ‘Script’, we can manually control the fixed update using ‘Physics.Simulate’. Here, we observe the differences when ‘Physics.simulationMode’ is placed at the top or bottom of the ‘FixedUpdate()’ function.

First time:

private void Awake()
{
    Physics.simulationMode = SimulationMode.Script;
}

private void FixedUpdate()
{
    Debug.Log($"{this.transform.position.y}");
    Physics.Simulate(Time.fixedDeltaTime);
}

output:

10
9.996076
9.988228
9.976456
9.96076
9.94114
9.917596
9.890128
9.858736
9.82342
9.78418
9.741015
9.693928
9.642916
9.587979
9.529119
9.466335
9.399628
9.328996
9.254439

Second time:

private void Awake()
{
    Physics.simulationMode = SimulationMode.Script;
}

private void FixedUpdate()
{
    Physics.Simulate(Time.fixedDeltaTime);
    Debug.Log($"{this.transform.position.y}");
}

output:

9.996076
9.988228
9.976456
9.96076
9.94114
9.917596
9.890128
9.858736
9.82342
9.78418
9.741015
9.693928
9.642916
9.587979
9.529119
9.466335
9.399628
9.328996
9.254439
9.17596

When ‘Physics.Simulate’ is placed at the bottom, not logging the sphere’s initial altitude. ‘Physics.Simulate’ is just a function, the remaining code will be executed after it is completed.

Leave a Reply

Your email address will not be published. Required fields are marked *