Reset Physics

I have dice game that I would like to have predictable physics given a seed to my own random number generator. I am using Physics.simulate() called from Update loop as in example.

If I restart the editor and have the dice roll, they land exactly the same way every time. I can play an entire game and get the exact same score.

If I play one game and then try to roll the dice using the exact same seed they behave close but slightly different. Not the same result as when I restart the emulator.

I have tried many combinations of making the rigid bodies kinematic, resetting their velocity and angular velocity etc.

I have turned Physics.autosimulate on then off again.

I have Logged all the values of the dice game objects and and they are all identical except the RigidBody.rotation quaternion, but when printed out they have the same Values and Euler values.

Is there a Physics.HardReset() function that can go through and set all calculations back to 0 like starting a PhysicsScene from scratch, just like starting the editor over?

I am using Unity 2018.4.

What you’re taking about is usually referred to as deterministic physics. To my knowledge you may be able to achieve this base on an article I found (linked below), but the article is for 2D Physics. Last time I looked into it, the Unity Devs are attempting to make deterministic physics, but it isn’t release yet.

Here’s a quote from the article which might be of some help. More steps can be found in the article.

“To perform a soft restart, you must ensure that each simulation starts with no physics components enabled, so that Unity doesn’t automatically add them to the physics world. You then need to manually enable or instantiate each physics component, in the same order each time. In theory, this results in Unity adding physics behavior to the physics world in the same order each time. As long as you enable the physics components in the same order, Unity should maintain the correct internal ordering.”

Link to Determinism with 2D Physics