//
Physics.Simulate
Leave feedback
Declaration
public static void Simulate(float step);
Parameters
step | The time to advance physics by. |
Description
Simulate physics in the Scene.
Call this to simulate physics manually when the automatic simulation is turned off. Simulation includes all the stages of collision detection, rigidbody and joints integration, and filing of the physics callbacks (contact, trigger and joints). Calling Physics.Simulate does not cause FixedUpdate to be called. MonoBehaviour.FixedUpdate will still be called at the rate defined by Time.fixedDeltaTime whether automatic simulation is on or off, and regardless of when you call Physics.Simulate.
Note that if you pass framerate-dependent step values (such as Time.deltaTime) to the physics engine, your simulation will be non-deterministic because of the unpredictable fluctuations in framerate that can arise.
To achieve deterministic physics results, you should pass a fixed step value to Physics.Simulate every time you call it. Usually, step
should be a small positive number. Using step
values greater than 0.03 is likely to produce inaccurate results.
See Also: Physics.autoSimulation.
Here is an example of a basic simulation that implements what's being done in the automatic simulation mode (excluding Time.maximumDeltaTime).
using UnityEngine; public class BasicSimulation : MonoBehaviour { private float timer; void Update() { if (Physics.autoSimulation) return; // do nothing if the automatic simulation is enabled timer += Time.deltaTime; // Catch up with the game time. // Advance the physics simulation in portions of Time.fixedDeltaTime // Note that generally, we don't want to pass variable delta to Simulate as that leads to unstable results. while (timer >= Time.fixedDeltaTime) { timer -= Time.fixedDeltaTime; Physics.Simulate(Time.fixedDeltaTime); } // Here you can access the transforms state right after the simulation, if needed } }
///
Time.fixedDeltaTime
Leave feedback
public static float fixedDeltaTime;
Description
The interval in seconds at which physics and other fixed frame rate updates (like MonoBehaviour's FixedUpdate) are performed.
Unity does not adjust fixedDeltaTime based on Time.timeScale. The fixedDeltaTime interval is always relative to the in-game time which Time.timeScale affects.
See Time and Frame Rate Management in the User Manual for more information about how this property relates to the other Time properties.
/
Physics.autoSimulation
Leave feedback
public static bool autoSimulation;
Description
Sets whether the physics should be simulated automatically or not.
By default, physics is updated every Time.fixedDeltaTime during the play mode. It happens automatically as part of the regular game loop.
However, there are cases where being able to advance physics manually is needed. One particular example simulating physics in the edit mode. Another example could be networked physics where rewinding time back and applying all the player input is required up on receiving data from the authoritative server.
To control the physics simulation manually, disable the automatic simulation first and then use Physics.Simulate to advance time. Note that MonoBehaviour.FixedUpdate will still be called at the rate defined by Time.fixedDeltaTime, but the physics simulation will no longer be advanced automatically.
///
Physics.SyncTransforms
Leave feedback
Declaration
public static void SyncTransforms();
Description
Apply Transform changes to the physics engine.
When a Transform component changes, any Rigidbody or Collider on that Transform or its children may need to be repositioned, rotated or scaled depending on the change to the Transform. Use this function to flush those changes to the physics engine manually.
///
Collider等附加到GameObject的Transform没有反映在物理引擎中,在
Physics.CheckSphere() Physics.Simulate()前
调用Physics.SyncTransforms()
/