Manually set physics simulation
Control over when physics calculations occur to align them with game performance.
Read time 2 minutesLast updated 13 days ago
Control the physics simulation manually to decide when physics calculations occur to align physics updates with your application's actual performance and avoid redundant updates. This can help optimize performance during busy frames and is beneficial for scenarios that require tight synchronization with game logic or custom physics update loops.
Manually control the physics simulation to:
- Ensure stability and consistency: Physics systems are designed for small, consistent time steps. Variable time step values, such as , can cause unstable simulations, and lead to issues such as objects passing through each other, a phenomenon known as tunneling, at low frame rates or jittery behavior when frame rates fluctuate.
Time.deltaTime - Create reproducible behavior: Using a fixed step is crucial for more reproducible physics behavior (though true determinism with PhysX has other challenges).
- Avoid large, inaccurate steps: If frame rates drop, increases. Simulating a physics step with a large, variable delta can be computationally resource intensive and less accurate, potentially worsening performance issues.
Time.deltaTime
Use (or ) to manually control the physics simulation and in specific, local instances. Refer to the Multi-Scene Physics learn tutorial to learn more about multi-scene physics.
Physics.Simulate(float stepTime)yourPhysicsScene.Simulate(float stepTime)PhysicsSceneWhen you call , the recommended best practice is to use a fixed time step, rather than directly passing . For example, use the same value that you might use in the ' value, such as 0.02 for 50Hz.
Physics.SimulateTime.deltaTimeProject Settings
?
Fixed Timestep
?
If you manually simulate physics in a scene using , ensure that automatic simulation is disabled. You can do this by setting the Simulation Mode to Script. Once in Script mode (either through Project Settings or script), you must call for the scene at a selected frequency with an appropriate, preferably fixed, time step.
Physics.Simulate()Physics.SimulateTo disable automatic simulation, in the Editor:
- Select Edit > Project Settings to open the Project Settings window.
- Select the Physics > Settings tab.
- Select the tab.GameObject
?
- Set Simulation Mode to Script.
To disable automatic simulation, in script, set . Typically, you do this at the start of your game, before any physics simulation occurs.
Physics.simulationMode = SimulationMode.Script;