# Distance

> Returns the distance between a and b.

## Definition

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

## Distance(Vector2, Vector2)

Returns the distance between `a` and `b`.

```csharp
public static float Distance(Vector2 a, Vector2 b)
```

### Parameters

**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): First position to compare distance from.**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): Second position to compare distance from.

### Returns

| Type                                                          | Description                                                              |
| ------------------------------------------------------------- | ------------------------------------------------------------------------ |
| [float](https://learn.microsoft.com/dotnet/api/system.single) | The distance between the two positions, as an absolute (positive) value. |

### Remarks

The distance between two `Vector` objects is calculated as the magnitude of the `Vector` resulting from vectorA − vectorB.

VectorB − vectorA gives the same result. In other words, `Vector2.Distance(a,b)` is the same as `(a-b).magnitude`.

You usually use this method is to find the distance between two entities.

For example, here an enemy will attack the player if they get within a certain range. The script below should be attached to an enemy `GameObject`.

### Examples

```csharp
using UnityEngine;

public class DistanceExample_enemy : MonoBehaviour
{
    // Add a reference to the player, which is set in the Inspector window
    public GameObject player;

    // Set the distance at which the enemy attacks
    public float attackDistanceThreshold = 2f;

    void Update()
    {
        // Take the position of the player, and the position of this GameObject
        Vector2 playerPosition = player.transform.position;
        Vector2 myPosition = transform.position;

        // Use <see cref="UnityEngine.Vector2.Distance"></see> to obtain the distance between the player position and this GameObject's position
        float distance = Vector2.Distance(myPosition, playerPosition);

        // Check if the player is close enough
        if(distance < attackDistanceThreshold)
        {
            Attack(player);
        }
    }

    void Attack(GameObject target)
    {
        // Insert the attack logic here
        Debug.Log($"{name} attacks {target.name}");
    }
}
```

## Distance(Vector2, Vector2)

Returns the distance between `a` and `b`.

```csharp
public static float Distance(in Vector2 a, in Vector2 b)
```

### Parameters

**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): First position to compare distance from.**** (\[Vector2]\(/engine/6000.7/script-reference/unityengine/vector2)): Second position to compare distance from.

### Returns

| Type                                                          | Description                                                              |
| ------------------------------------------------------------- | ------------------------------------------------------------------------ |
| [float](https://learn.microsoft.com/dotnet/api/system.single) | The distance between the two positions, as an absolute (positive) value. |

### Remarks

The distance between two `Vector` objects is calculated as the magnitude of the `Vector` resulting from vectorA − vectorB.

VectorB − vectorA gives the same result. In other words, `Vector2.Distance(a,b)` is the same as `(a-b).magnitude`.

You usually use this method is to find the distance between two entities.

For example, here an enemy will attack the player if they get within a certain range. The script below should be attached to an enemy `GameObject`.

### Examples

```csharp
using UnityEngine;

public class DistanceExample_enemy : MonoBehaviour
{
    // Add a reference to the player, which is set in the Inspector window
    public GameObject player;

    // Set the distance at which the enemy attacks
    public float attackDistanceThreshold = 2f;

    void Update()
    {
        // Take the position of the player, and the position of this GameObject
        Vector2 playerPosition = player.transform.position;
        Vector2 myPosition = transform.position;

        // Use <see cref="UnityEngine.Vector2.Distance"></see> to obtain the distance between the player position and this GameObject's position
        float distance = Vector2.Distance(myPosition, playerPosition);

        // Check if the player is close enough
        if(distance < attackDistanceThreshold)
        {
            Attack(player);
        }
    }

    void Attack(GameObject target)
    {
        // Insert the attack logic here
        Debug.Log($"{name} attacks {target.name}");
    }
}
```
