Version: 5.6 (switch to 2017.1b)
LanguageEnglish
  • C#
  • JS

Script language

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

NavMesh.SamplePosition

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

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.

Close

Cancel

public static function SamplePosition(sourcePosition: Vector3, out hit: AI.NavMeshHit, maxDistance: float, areaMask: int): bool;
public static bool SamplePosition(Vector3 sourcePosition, out AI.NavMeshHit hit, float maxDistance, int areaMask);

Parameters

sourcePosition The origin of the sample query.
hit Holds the properties of the resulting location.
maxDistance Sample within this distance from sourcePosition.
areaMask A mask specifying which NavMesh areas are allowed when finding the nearest point.

Returns

bool True if a nearest point is found.

Description

Finds the closest point on NavMesh within specified range.

The function samples the NavMesh to find the closest point on the NavMesh.

The closest point is returned based on distance to the query point. The function does not check for obstruction in the world. For example, you the sourcePosition is on the ceiling, a point on the second floor will be returned (if there is NavMesh there), instead of floor position on the first floor.

The function can get quite expensive if the search radius is really big. A good starting point for the maxDistance is 2 times the agent height.

If you are trying to find a random point on the NavMesh, it is better to use recommended radius and do try multiple times instead of using one very large radius.

#pragma strict
// RandomPointOnNavMesh
public class RandomPointOnNavMesh extends MonoBehaviour {
	public var range: float = 10.0f;
	function RandomPoint(center: Vector3, range: float, result: Vector3) {
		for (var i: int = 0; i < 30; i++) {
			var randomPoint: Vector3 = center + Random.insideUnitSphere * range;
			var hit: NavMeshHit;
			if (NavMesh.SamplePosition(randomPoint, hit, 1.0f, NavMesh.AllAreas)) {
				result = hit.position;
				return true;
			}
		}
		result = Vector3.zero;
		return false;
	}
	function Update() {
		var point: Vector3;
		if (RandomPoint(transform.position, range, point)) {
			Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
		}
	}
}
// RandomPointOnNavMesh
using UnityEngine;
using UnityEngine.AI;

public class RandomPointOnNavMesh : MonoBehaviour { public float range = 10.0f;

bool RandomPoint(Vector3 center, float range, out Vector3 result) { for (int i = 0; i < 30; i++) { Vector3 randomPoint = center + Random.insideUnitSphere * range; NavMeshHit hit; if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas)) { result = hit.position; return true; } } result = Vector3.zero; return false; }

void Update() { Vector3 point; if (RandomPoint(transform.position, range, out point)) { Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f); } } }

public static function SamplePosition(sourcePosition: Vector3, out hit: AI.NavMeshHit, maxDistance: float, filter: AI.NavMeshQueryFilter): bool;
public static bool SamplePosition(Vector3 sourcePosition, out AI.NavMeshHit hit, float maxDistance, AI.NavMeshQueryFilter filter);

Parameters

sourcePosition The origin of the sample query.
hit Holds the properties of the resulting location.
maxDistance Sample within this distance from sourcePosition.
filter A filter specifying which NavMesh areas are allowed when finding the nearest point.

Returns

bool True if a nearest point is found.

Description

Samples the position closest to sourcePosition - on any NavMesh built for the agent type specified by the filter.

Consider only positions on areas defined in the NavMeshQueryFilter.areaMask. A maximum search radius is set by maxDistance. The information of any found position is returned in the hit argument.