Hi
What is the best way nowdays to pause or freeze the physics simulation?
We dont want to use Time.timescale as it freezes everything in the game, we just want the physics. Any ideas?
Thanks!
@argibaltzi did you find any nice solution? I have one, but it’s quite dirty by my opinion.
For me the biggest problem was that after unpausing the game, physics simulated multiple steps in one frame adding enormous velocity to rigid bodies.
This solution is quite dirty as it uses reflection, but it works for me. As you can see that after enabling the physics group, I reset the time of the group to the current time (for me, I can’t pause all Update group, as I have custom rendering systems, which I want to be active during pause)
From the PauseController script
var fixedStep = World.DefaultGameObjectInjectionWorld
.GetOrCreateSystemManaged<FixedStepSimulationSystemGroup>();
fixedStep.Enabled = isPlaying;
if (isPlaying)
{
fixedStep.ResetLastFixedUpdateTime();
}
And here is the extension method to reset LastExecutionTime
public static void ResetLastFixedUpdateTime(this FixedStepSimulationSystemGroup group)
{
var manager = group.RateManager;
var type = typeof(RateUtils.FixedRateCatchUpManager);
type
.GetField("m_LastFixedUpdateTime", BindingFlags.Instance | BindingFlags.NonPublic)
!.SetValue(manager, group.World.Time.ElapsedTime - group.Timestep);
}