Physics.Simulate

Hello there,

Today I was experimenting with this and I got a bit confused.
I thought this would allow us to call the simulation whenever we would like and with a variable time step and have it fully sync with the normal update.

I’m currently calling it like this:

void Update () {
        Physics.Simulate(Time.time - m_timer);
        m_timer = Time.time;
    }
}

And most of the times I get 2 calls to the FixedUpdates inside my scripts for each normal Update loop.
Is that the expected behaviour? Is there a possibility of syncing this with the normal Update so the FixedUpdates are called only once?

Thanks!

Unity - Scripting API: Physics.autoSimulation Turned off auto fixed update?

yep, sorry for to mention that in my previous email. This is the Start of the same component of the update:

void Start () {
        Physics.autoSimulation = false;
        m_timer = Time.time;

    }

EDIT: The funny thing is that if I modify the Fixed TimeStep in the Project Setting it seems to modify the number of times FixedUpdate is called.

Physics2D.Simulate has nothing whatsoever to do with FixedUpdate callbacks as it says in the docs.

FixedUpdate is just a callback, it isn’t directly related to physics; it just gives you a callback with a fixed timestep. It’ll still get called more than once if it needs to catch up. FixedUpdate will still happen even if you turn off the auto-simulation. It’s just that physics won’t be called during the fixed update and you are left to call it yourself either in the fixed update or per-frame as you’re doing here.

Doing it per-frame will be more expensive as you’re potentially calling it much more often and you’ll not get a consistent simulation as it’s a variable time so calculations will be different each time depending on frame-rate. If that’s not a problem then you can do this.

Also, if you’re doing it per-frame then don’t use interpolation on Rigidbody as it’s a waste as interpolation is not used.

2 Likes

Thanks for the clarification, that explains things!
I’ve always seen the FixedUpdate tighted to the Physics so I thought that would be still connected.

So, if you don´t update Physics.Simulate at a Time.fixedDeltaTime rate, then FixedUpdate is a completely disconnected callback with no specific purpose, am I correct assuming that?

It’s more subtle than that. What you think of as FixedUpdate is simply the engine calling “MonoBehaviour.FixedUpdate” (scripts) each fixed interval. After those have been called, with “autoSimulate=true” then the engine implicitly simulates physics with the fixed time interval. If you set “autoSimulate=false” then the engine still calls “MonoBehaviour.FixedUpdate” but it doesn’t simulate physics at all and leaves that to you. You are free to call it during the “MonoBehaviour.FixedUpdate” yourself with a fixed time interval or during “MonoBehaviour.Update” (per-frame) with the frame time interval.

You can see this here as a diagram: Unity - Manual: Order of execution for event functions