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

Script language

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


Suggest a change


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.


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.



public method FindClosestEdge(out hit: AI.NavMeshHit): bool;
public bool FindClosestEdge(out AI.NavMeshHit hit);


hit Holds the properties of the resulting location.


bool True if a nearest edge is found.


Locate the closest NavMesh edge.

The returned NavMeshHit object contains the position and details of the nearest point on the nearest edge of the Navmesh. Since an edge typically corresponds to a wall or other large object, this could be used to make a character take cover as close to the wall as possible.

#pragma strict

private var agent: UnityEngine.AI.NavMeshAgent;

function Start () { agent = GetComponent.<UnityEngine.AI.NavMeshAgent>(); }

function Update() { // Move to the nearest wall when the mouse is clicked. if (Input.GetMouseButtonDown(0)) { TakeCover(); } }

function TakeCover() { var hit: UnityEngine.AI.NavMeshHit;

if (agent.FindClosestEdge(hit)) { agent.SetDestination(hit.position); } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { private AI.NavMeshAgent agent; void Start() { agent = GetComponent<AI.NavMeshAgent>(); } void Update() { if (Input.GetMouseButtonDown(0)) TakeCover(); } void TakeCover() { AI.NavMeshHit hit; if (agent.FindClosestEdge(out hit)) agent.SetDestination(hit.position); } }

Did you find this page useful? Please give it a rating: