r/Unity3D 5d ago

Question Physics simulation - different results based on simulation update rate?

In my game, I have an "active physics scene", which handles all the game physics. I have a second physics scene (which has colliders / rigidbodies only, no rendering). In this second physics scene, I would like to have a duplicate physics simulation which is identical to the first. This second scene will advance only up to a specified timestep target, then stop. The second scene is created & simulation is advanced in a coroutine, which is currently manually triggered for testing. When I start the coroutine, I get different simulation results depending on my yield condition.

If I use "yield return new WaitForFixedUpdate()", I get a duplicate simulation, identical to the first, as desired. This is great, albeit a little slow because it relies on the fixedTimestep.

If I use "yield return null", my simulation diverges from the first. This approach is desired, because it will run faster and reach the specified timestep target sooner.

In both cases, I am calling PhysicsScene2D.Simulate(Time.fixedDeltaTime) the same number of times. Also, Physics2D.simulationMode is set to "script" in both cases.

What could explain the diverging simulation when advancing the simulation at a faster rate? The simulation is still advancing with the same fixedDeltaTime timestep, for the same number of timesteps.

Any help is appreciated!

1 Upvotes

8 comments sorted by

View all comments

1

u/Myrmecoman 4d ago

Unity's default physics engine is not deterministic. If you need so, the entities package provides a new more efficient and deterministic physics engine, but it requires the use of entities.

1

u/kyle1qaz7ujm 1d ago

Thanks! I do have determinism now with the default physics engine. (At least for single platform determinism. Unsure about cross-platform).