public static void Simulate (float step);

参数

step推进物理模拟的时间。

描述

在场景中进行物理模拟。

当关闭自动模拟时,可以调用该函数手动进行物理模拟。模拟包括碰撞检测、刚体和关节整合的所有阶段,以及物理回调(接触、触发和关节)的归档。调用 Physics.Simulate 不会导致调用 FixedUpdate。无论自动模拟关闭与否以及您何时调用 Physics.Simulate,都仍将以 Time.fixedDeltaTime 定义的速率调用 MonoBehaviour.FixedUpdate

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

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

另请参阅:Physics.autoSimulation

下面是一个基本模拟示例,它实现了在自动模拟模式下完成的操作(不包括 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 } }