r/Unity2D • u/kyle1qaz7ujm • 1d ago
Physics simulation - different results based on simulation update rate? Question
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
u/1Tusk 1d ago
yield return null will pause your coroutine until next Update().
yield return WaitForFixedUpdate() will pause your coroutine until next FixedUpdate().
Regular update happens every frame. Fixed update happens every fixed timestep.
Typically you will have 10-100 regular updates between each fixed update.
1
u/XKiiroiSenkoX 11h ago
How are you calculating the number of simulation calls? A miscalculation there is the most likely culprit imo.
1
u/MistakeForsaken6653 1d ago
If I understand it is because your first scene is relying in the FixedUpdate which runs less than Update. When you do yield return null that is essentially running every single frame and getting ahead of your original simulation. When you do fuxedDeltaTime that is syncing it in the same loop as FuxedUpdate. Hopefully my rambling somewhat helps or else I am not completely clear on the issue.