# Simulate(float, int)

> Simulate physics in the default physics scene.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.Physics2DModule

```csharp
public static bool Simulate(float deltaTime, int simulationLayers = -1)
```

### Parameters

**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The time to advance physics by.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The [Rigidbody2D](/engine/6000.7/script-reference/unityengine/rigidbody2d.md) and [Collider2D](/engine/6000.7/script-reference/unityengine/collider2d.md) layers to simulate.

### Returns

| Type                                                          | Description                                                                                              |
| ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Whether the simulation was run or not. Running the simulation during physics callbacks will always fail. |

### Remarks

Calling [Physics2D.Simulate](/engine/6000.7/script-reference/unityengine/physics2d/simulate.md) will perform a single simulation step, simulating physics over the specified `step` time.

By default, [All Layers](/engine/6000.7/script-reference/unityengine/physics2d/alllayers.md) are simulated, however if you specify layers then only the [Rigidbody2D](/engine/6000.7/script-reference/unityengine/rigidbody2d.md) on those layers will be simulated. Along with this, only contacts for [Collider2D](/engine/6000.7/script-reference/unityengine/collider2d.md) on the specified layer(s) will be handled. Finally, only [joints](/engine/6000.7/script-reference/unityengine/joint2d.md) or [effectors](/engine/6000.7/script-reference/unityengine/effector2d.md) on the specified layer(s) will be handled.

You can call [Physics2D.Simulate](/engine/6000.7/script-reference/unityengine/physics2d/simulate.md) in the Editor outside of play mode, however caution is advised as this will cause the simulation to move GameObjects that have an attached [Rigidbody2D](/engine/6000.7/script-reference/unityengine/rigidbody2d.md) component. When simulating in the Editor outside of play mode, a full simulation occurs for all physics components including [Rigidbody2D](/engine/6000.7/script-reference/unityengine/rigidbody2d.md), [Collider2D](/engine/6000.7/script-reference/unityengine/collider2d.md), [Joint2D](/engine/6000.7/script-reference/unityengine/joint2d.md) and [Effector2D](/engine/6000.7/script-reference/unityengine/effector2d.md), and contacts are generated. However, contacts are not reported via the standard script callbacks. This is a safety measure to prevent callbacks from deleting objects in the Scene, which is not an undoable operation.

**NOTE:** Calling [Physics2D.Simulate](/engine/6000.7/script-reference/unityengine/physics2d/simulate.md) does not cause Unity to call FixedUpdate. Unity still calls `FixedUpdate` at the rate defined by [\_fixedDeltaTime](/engine/6000.7/script-reference/unityengine/time/fixeddeltatime.md) whether automatic simulation is on or off, and regardless of when you call Physics2D.Simulate. Also, if you pass framerate-dependent step values (such as [\_deltaTime](/engine/6000.7/script-reference/unityengine/time/deltatime.md)) 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.

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

Additional Resources: [\_simulationMode](/engine/6000.7/script-reference/unityengine/physics2d/simulationmode.md), [PhysicsScene2D.Simulate](/engine/6000.7/script-reference/unityengine/physicsscene2d/simulate.md)

### Examples

```csharp
using UnityEngine;

public class BasicSimulation : MonoBehaviour
{
    private float timer;

    void Update()
    {
        if (Physics2D.simulationMode != SimulationMode2D.Script)
            return; // do nothing if simulation is not set to be executed via script.

        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...
    }
}
```
