Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Physics2D.Simulate

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method Simulate(step: float): bool;
public static bool Simulate(float step);

Parameters

step The time to advance physics by.

Returns

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

Description

Simulate physics in the scene.

Calling Physics2D.Simulate does not cause FixedUpdate to be called. MonoBehaviour.FixedUpdate will still be called at the rate defined by Time.fixedDeltaTime whether automatic simulation is on or off, and regardless of when you call Physics2D.Simulate.

Note that 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 Physics2D.Simulate every time you call it.

You can call Physics2D.Simulate in the Editor outside of play mode however caution is advised as this will cause the simulation to move GameObject that have a Rigidbody2D component attached. When simulating in the Editor outside of play mode, a full simulation occurs for all physics components including Rigidbody2D, Collider2D, Joint2D and Effector2D 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.

See Also: Physics2D.autoSimulation.

Here is an example of a basic simulation that implements what's being done in the automatic simulation mode.

#pragma strict
public class BasicSimulation extends MonoBehaviour {
	private var timer: float;
	function Update() {
		if (Physics2D.autoSimulation)return ;
		// do nothing if the automatic simulation is enabled
		timer += Time.deltaTime;
		while ( timer >= Time.fixedDeltaTime ) {
			timer -= Time.fixedDeltaTime;
			Physics2D.Simulate(Time.fixedDeltaTime);
		}
		// Here you can access the transforms state right after the simulation, if needed...
	}
}
using UnityEngine;

public class BasicSimulation : MonoBehaviour { private float timer;

void Update() { if (Physics2D.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; Physics2D.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: