Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

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

NavMesh.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 static function FindClosestEdge(sourcePosition: Vector3, out hit: NavMeshHit, areaMask: int): bool;
public static bool FindClosestEdge(Vector3 sourcePosition, out NavMeshHit hit, int areaMask);

Parameters

sourcePosition The origin of the distance query.
hit Holds the properties of the resulting location.
areaMask A bitfield mask specifying which NavMesh areas can be passed when finding the nearest edge.

Returns

bool True if a nearest edge is found.

Description

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

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.

#pragma strict
// MeasureSpaceAround.cs
public class MeasureSpace extends MonoBehaviour {
	function DrawCircle(center: Vector3, radius: float, color: Color) {
		var prevPos: Vector3 = center + new Vector3(radius, 0, 0);
		for (var i: int = 0; i < 30; i++) {
			var angle: float = float(i + 1) / 30.0f * Mathf.PI * 2.0f;
			var newPos: Vector3 = center + new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius);
			Debug.DrawLine(prevPos, newPos, color);
			prevPos = newPos;
		}
	}
	function Update() {
		var hit: NavMeshHit;
		if (NavMesh.FindClosestEdge(transform.position, hit, NavMesh.AllAreas)) {
			DrawCircle(transform.position, hit.distance, Color.red);
			Debug.DrawRay(hit.position, Vector3.up, Color.red);
		}
	}
}
// MeasureSpaceAround.cs
using UnityEngine;
using System.Collections;
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);
		}
	}
}