# Distance

> Calculates the minimum distance of this collider against all Collider2D attached to this Rigidbody2D.

## Definition

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

## Distance(Collider2D)

Calculates the minimum distance of this `collider` against all [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md).

```csharp
public ColliderDistance2D Distance(Collider2D collider)
```

### Parameters

**** (\[Collider2D]\(/engine/6000.3/script-reference/unityengine/collider2d)): A collider used to calculate the minimum distance against all colliders attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md).

### Returns

| Type                                                                                    | Description                                                                                                                                          |
| --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ColliderDistance2D](/engine/6000.3/script-reference/unityengine/colliderdistance2d.md) | The minimum distance of `collider` against all colliders attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md). |

### Remarks

The provided `collider` will be check against all [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md) and the minimum distance from all attached [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) will be returned.

The provided `collider` and at least one [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md) must be valid for the returned [ColliderDistance2D](/engine/6000.3/script-reference/unityengine/colliderdistance2d.md) to be valid i.e. the [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) should not be disabled and must contain collision shapes and the provided `collider` must not be NULL.  You can check if the returned value is valid by checking [ColliderDistance2D.isValid](/engine/6000.3/script-reference/unityengine/colliderdistance2d/isvalid.md).

Additional Resources: [Physics2D.Distance](/engine/6000.3/script-reference/unityengine/physics2d/distance.md) and [Collider2D.Distance](/engine/6000.3/script-reference/unityengine/collider2d/distance.md).

## Distance(Vector2, float, Collider2D, Vector2, float)

Calculates the minimum distance of this `collider` against all [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md).

```csharp
public ColliderDistance2D Distance(Vector2 thisPosition, float thisAngle, Collider2D collider, Vector2 position, float angle)
```

### Parameters

**** (\[Vector2]\(/engine/6000.3/script-reference/unityengine/vector2)): The position to use for this Rigidbody.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The rotation to use for this Rigidbody.**** (\[Collider2D]\(/engine/6000.3/script-reference/unityengine/collider2d)): A collider used to calculate the minimum separation against this Rigidbody.**** (\[Vector2]\(/engine/6000.3/script-reference/unityengine/vector2)): The position to use for the specified `collider`.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The rotation to use for the specified `collider`.

### Returns

| Type                                                                                    | Description                                                                                                                                          |
| --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ColliderDistance2D](/engine/6000.3/script-reference/unityengine/colliderdistance2d.md) | The minimum distance of `collider` against all colliders attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md). |

### Remarks

The provided `collider` will be check against all [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md) and the minimum distance from all attached [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) will be returned.

The provided `collider` and at least one [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) attached to this [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md) must be valid for the returned [ColliderDistance2D](/engine/6000.3/script-reference/unityengine/colliderdistance2d.md) to be valid i.e. the [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) should not be disabled and must contain collision shapes and the provided `collider` must not be NULL.  You can check if the returned value is valid by checking [ColliderDistance2D.isValid](/engine/6000.3/script-reference/unityengine/colliderdistance2d/isvalid.md).

**NOTE**: The positions and angles used here represent the position of the [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md) the respective [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) is attached to. If the [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) is offset from the center of mass then the [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) will use the same offset. This can be confusing so it is recommened that only [Collider2D](/engine/6000.3/script-reference/unityengine/collider2d.md) that align with the center of mass are used. If not then you must take this into account. If the specified `collider` is not attached to a [Rigidbody2D](/engine/6000.3/script-reference/unityengine/rigidbody2d.md), this call cannot be used and will result in a warning.

Additional Resources: [Physics2D.Distance](/engine/6000.3/script-reference/unityengine/physics2d/distance.md) and [Collider2D.Distance](/engine/6000.3/script-reference/unityengine/collider2d/distance.md).
