这比手动设置Transform.position和Transform.rotation更有效率。
注意:此方法不会更新子 Transform。它应该从最顶层的 Transform 调用,向下遍历层次结构。
using UnityEngine;
public class PositionTracker : MonoBehaviour { private PhysicsScene m_SomeScene; private Rigidbody m_Rigidbody;
public void SimulateTrajectory(float totalTime) { m_SomeScene.RunSimulationStages(0f, SimulationStage.PrepareSimulation);
for (int i = 0; i < totalTime / Time.fixedDeltaTime; i++) m_SomeScene.RunSimulationStages(Time.fixedDeltaTime, SimulationStage.RunSimulation);
// Transform of the m_Rigidbody is still like it was at the start of the method m_Rigidbody.PublishTransform(); // Transform of the m_Rigidbody is now up to date and can be accessed to see where the body ended up after simulating for "totalTime" seconds } }
使用Rigidbody模拟PhysicsScene,并使用PublishTransform将物理系统中的信息读取到Transform组件。