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

パラメーター

sourcePosition クエリとなるサンプルの原点
hit 一番近い辺のヒット情報
maxDistance サンプリングされる sourcePosition からの距離
areaMask もっとも近い位置を見つけるときに特定のマスクをかけるためにナビメッシュレイヤーを渡すことができる

戻り値

bool もっとも近い点が見つかった場合は true

説明

指定した範囲内の NavMesh で最も近い点を検索します

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

最も近い点はクエリポイントへの距離に基づいて返されます。関数はワールド内の閉塞をチェックしません。たとえば sourcePosition が天井にあり、1 階の床の位置ではなく 2 階上の点が返されます ( NavMesh がそこにある場合) 。

検索半径が本当に大きい場合、関数は非常に高いコストがかかります。 MaxDistance のよい出発点はエージェントの高さの 2 倍です。

NavMesh でランダムな点を検索する場合、ひとつの大きな半径を使用する代わりに推奨される半径を複数回試すことをお勧めします。

// 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 bool SamplePosition (Vector3 sourcePosition, out AI.NavMeshHit hit, float maxDistance, AI.NavMeshQueryFilter filter);

パラメーター

sourcePosition クエリとなるサンプルの原点
hit 一番近い辺のヒット情報
maxDistance サンプリングされる sourcePosition からの距離
filter A filter specifying which NavMesh areas are allowed when finding the nearest point.

戻り値

bool もっとも近い点が見つかった場合は true

説明

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.