Unity怎样只使用Unity内部的API获取NavMeshPath的corners中的某个元素是否是NavMeshLink的startPos或者endPos请不要使用第三方的API
可以使用NavMesh.CalculatePath方法获取NavMeshPath,然后遍历path.corners数组,判断每个元素是否位于NavMeshLink的startPos或者endPos。具体实现代码如下:
NavMeshPath path = new NavMeshPath();
NavMesh.CalculatePath(startPos, endPos, NavMesh.AllAreas, path);
for (int i = 0; i < path.corners.Length; i++)
{
NavMeshHit hit;
if (NavMesh.SamplePosition(path.corners[i], out hit, 0.1f, NavMesh.AllAreas))
{
NavMeshLink link = NavMesh.FindClosestLink(hit.position, NavMesh.AllAreas);
if (link != null && (link.startPosition == hit.position || link.endPosition == hit.position))
{
Debug.Log("The " + i + "th corner is a NavMeshLink start or end position.");
}
}
}
其中,NavMesh.SamplePosition方法用于将path.corners数组中的点投射到NavMesh上,得到离该点最近的NavMesh上的点,然后使用NavMesh.FindClosestLink方法找到该点所属的NavMeshLink,最后判断该点是否是NavMeshLink的startPos或者endPos。
原文地址: https://www.cveoy.top/t/topic/bS5Y 著作权归作者所有。请勿转载和采集!