step | 推进物理模拟的时间。 |
void 是否正在运行模拟。物理回调期间无法成功运行模拟。
进行与此 PhysicsScene 关联的物理模拟。
调用此方法会导致在指定的 step
时间内的物理模拟。只会进行与此 PhysicsScene 关联的物理模拟。如果此 PhysicsScene 不是默认的物理场景(请参阅 Physics.defaultPhysicsScene),那么它与特定的 Scene 相关联,因此,只有添加到该 Scene 的组件才会在运行模拟时受到影响。
如果将与帧率相关的步长值(例如 Time.deltaTime)传递给物理引擎,则由于可能出现的不可预测的帧率波动,模拟的确定性将降低。要获得确定性的物理结果,每次调用 PhysicsScene.Simulate 时都应该向其传递一个固定的步长值。
即使不处于播放模式下,也可以在 Editor 中调用 PhysicsScene.Simulate,但要务必小心,因为这将导致模拟移动附加了 Rigidbody 组件的游戏对象。在非播放模式下的 Editor 中进行模拟时,将对所有物理组件(包括 Rigidbody、Collider 和 Joint)进行完整的模拟,并会生成接触点,但不通过标准脚本回调报告接触点。这是一种安全措施,可防止回调删除场景中的对象(这是一种不可撤销的操作)。
下面是一个基本模拟示例,它实现了在自动模拟模式下完成的操作。
using UnityEngine;
public class BasicSimulation : MonoBehaviour { public PhysicsScene physicsScene; private float timer;
void Update() { if (!physicsScene.IsValid()) return; // do nothing if the physics Scene is not valid.
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; physicsScene.Simulate(Time.fixedDeltaTime); }
// Here you can access the transforms state right after the simulation, if needed... } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.