public void Simulate (float step);

Parameters

stepThe time to advance physics by.

Returns

void Whether the simulation was run or not. Running the simulation during physics callbacks will always fail.

Description

Simulate physics associated with this PhysicsScene.

Calling this method causes the physics to be simulated over the specified step time. Only the physics associated with this PhysicsScene will be simulated. If this PhysicsScene is not the default physics Scene (see Physics.defaultPhysicsScene) then it is associated with a specific Scene and as such, only components added to that Scene are affected when running the simulation.

If you pass framerate-dependent step values (such as Time.deltaTime) to the physics engine, your simulation will be less deterministic because of the unpredictable fluctuations in framerate that can arise. To achieve more deterministic physics results, you should pass a fixed step value to PhysicsScene.Simulate every time you call it.

You can call PhysicsScene.Simulate in the Editor outside of play mode however caution is advised as this will cause the simulation to move GameObject that have a Rigidbody component attached. When simulating in the Editor outside of play mode, a full simulation occurs for all physics components including Rigidbody, Collider and Joint including the generation of contacts however contacts are not reported via the standard script callbacks. This is a safety measure to prevent allowing callbacks to delete objects in the Scene which would not be an undoable operation. Here is an example of a basic simulation that implements what's being done in the automatic simulation mode.

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... } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961