NavMeshAgent.FindClosestEdge

function FindClosestEdge (out hit : NavMeshHit) : boolean

Parameters

NameDescription
hit Holds the properties of the resulting location.

Returns

boolean - True if a nearest edge is found.

Description

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.

JavaScript
private var agent: NavMeshAgent;
function Start () {
agent = GetComponent.<NavMeshAgent>();
}
function Update() {
// Move to the nearest wall when the mouse is clicked.
if (Input.GetMouseButtonDown(0)) {
TakeCover();
}
}
function TakeCover() {
var hit: NavMeshHit;

if (agent.FindClosestEdge(hit)) {
agent.SetDestination(hit.position);
}
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
private NavMeshAgent agent;
void Start() {
agent = GetComponent<NavMeshAgent>();
}
void Update() {
if (Input.GetMouseButtonDown(0))
TakeCover();

}
void TakeCover() {
NavMeshHit hit;
if (agent.FindClosestEdge(out hit))
agent.SetDestination(hit.position);

}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

private agent as NavMeshAgent

def Start():
agent = GetComponent[of NavMeshAgent]()

def Update():
if Input.GetMouseButtonDown(0):
TakeCover()

def TakeCover():
hit as NavMeshHit
if agent.FindClosestEdge(hit):
agent.SetDestination(hit.position)