Version: 2022.3
public static void Simulate (float step);

参数

step 推进物理模拟的时间。

描述

在场景中进行物理模拟。

Call this to simulate physics manually when the simulation mode is set to Script. 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 simulation mode is set to Script or not, and regardless of when you call Physics.Simulate.

注意,如果将与帧率相关的步长值(例如 Time.deltaTime)传递给物理引擎,则由于可能出现的不可预测的帧率波动,您的模拟将是不确定的。

要获得确定性的物理结果,每次调用 Physics.Simulate 时都应该向其传递一个固定的步长值。通常,step 应该是一个较小的正数。使用大于 0.03 的 step 值可能会产生不准确的结果。

See Also: Physics.simulationMode, SimulationMode.

Here is an example of a basic simulation that implements what's being done in the SimulationMode.FixedUpdate simulation mode (excluding Time.maximumDeltaTime).

using UnityEngine;

public class BasicSimulation : MonoBehaviour { private float timer;

void Update() { if (Physics.simulationMode != SimulationMode.Script) 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 } }