# FindClosestEdge

> Locate the closest NavMesh edge from a point on the NavMesh.

## Definition

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

## FindClosestEdge(Vector3, NavMeshHit, int)

Locate the closest NavMesh edge from a point on the NavMesh.

```csharp
public static bool FindClosestEdge(Vector3 sourcePosition, out NavMeshHit hit, int areaMask)
```

### Parameters

**** (\[Vector3]\(/engine/6000.3/script-reference/unityengine/vector3)): The origin of the distance query.**** (\[NavMeshHit]\(/engine/6000.3/script-reference/unityengine/ai/navmeshhit)): Holds the properties of the resulting location.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A bitfield mask specifying which NavMesh areas can be passed when finding the nearest edge.

### Returns

| Type                                                          | Description                        |
| ------------------------------------------------------------- | ---------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | True if the nearest edge is found. |

### Remarks

The returned [NavMeshHit](/engine/6000.3/script-reference/unityengine/ai/navmeshhit.md) object contains the position and details of the nearest point on the nearest edge of the navmesh. This can be used to query how much extra space there is around the agent.

### Examples

```csharp
// MeasureSpace
using UnityEngine;
using UnityEngine.AI;

public class MeasureSpace : MonoBehaviour
{
    void DrawCircle(Vector3 center, float radius, Color color)
    {
        Vector3 prevPos = center + new Vector3(radius, 0, 0);
        for (int i = 0; i < 30; i++)
        {
            float angle = (float)(i + 1) / 30.0f * Mathf.PI * 2.0f;
            Vector3 newPos = center + new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius);
            Debug.DrawLine(prevPos, newPos, color);
            prevPos = newPos;
        }
    }

    void Update()
    {
        NavMeshHit hit;
        if (NavMesh.FindClosestEdge(transform.position, out hit, NavMesh.AllAreas))
        {
            DrawCircle(transform.position, hit.distance, Color.red);
            Debug.DrawRay(hit.position, Vector3.up, Color.red);
        }
    }
}
```

## FindClosestEdge(Vector3, NavMeshHit, NavMeshQueryFilter)

Locate the closest NavMesh edge from a point on the NavMesh, subject to the constraints of the filter argument.

```csharp
public static bool FindClosestEdge(Vector3 sourcePosition, out NavMeshHit hit, NavMeshQueryFilter filter)
```

### Parameters

**** (\[Vector3]\(/engine/6000.3/script-reference/unityengine/vector3)): The origin of the distance query.**** (\[NavMeshHit]\(/engine/6000.3/script-reference/unityengine/ai/navmeshhit)): Holds the properties of the resulting location.**** (\[NavMeshQueryFilter]\(/engine/6000.3/script-reference/unityengine/ai/navmeshqueryfilter)): A filter specifying which NavMesh areas can be passed when finding the nearest edge.

### Returns

| Type                                                          | Description                        |
| ------------------------------------------------------------- | ---------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | True if the nearest edge is found. |

### Remarks

The returned NavMeshHit object contains the position and details of the nearest point on the nearest edge of the NavMesh. This can be used to query how much extra space there is around the agent.
