Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

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

NavMeshAgent.FindClosestEdge

Suggest a change

Success!

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.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function FindClosestEdge(hit: NavMeshHit): bool;
public bool FindClosestEdge(NavMeshHit hit);
public def FindClosestEdge(hit as NavMeshHit) as bool

Parameters

hit Holds the properties of the resulting location.

Returns

bool 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.

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 ExampleClass : 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

public class ExampleClass(MonoBehaviour):

	private agent as NavMeshAgent

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

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

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