要判断NavMeshPath的corners中的某个元素是否是NavMeshLink的startPos或者endPos,可以使用NavMesh.CalculatePath方法返回的NavMeshPath的“pathStatus”属性和“status”属性。如果路径的状态是“PathComplete”(表示路径已成功计算),则可以检查路径中的每个角落,看它们是否与NavMeshLink的startPos或endPos相同。可以使用NavMeshLink.GetLinks方法获取所有NavMeshLink,然后遍历它们以查找与corners中的元素相同的startPos或endPos。如果找到匹配项,则表示该元素是NavMeshLink的startPos或endPos。

以下是示例代码:

NavMeshPath path = new NavMeshPath();
if (NavMesh.CalculatePath(startPos, endPos, NavMesh.AllAreas, path))
{
    if (path.status == NavMeshPathStatus.PathComplete)
    {
        bool isStartPos = false;
        bool isEndPos = false;

        foreach (Vector3 corner in path.corners)
        {
            foreach (NavMeshLink link in NavMesh.CalculateLinkVisibleFromPoint(corner, 100.0f, NavMesh.AllAreas))
            {
                if (link.startPosition == corner)
                {
                    isStartPos = true;
                    break;
                }
                else if (link.endPosition == corner)
                {
                    isEndPos = true;
                    break;
                }
            }
        }

        if (isStartPos)
        {
            // the first corner of path is a NavMeshLink's startPos
        }
        else if (isEndPos)
        {
            // the last corner of path is a NavMeshLink's endPos
        }
        else
        {
            // neither the first nor the last corner of path is a NavMeshLink's startPos or endPos
        }
    }
    else
    {
        // path calculation failed
    }
}
Unity怎样获取NavMeshPath的corners中的某个元素是否是NavMeshLink的startPos或者endPos

原文地址: https://www.cveoy.top/t/topic/bS5p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录