即使在从Unity页面复制原始示例之后,我也无法让方法NavMesh.CalculatePath工作。https://docs.unity3d.com/ScriptReference/AI.NavMesh.CalculatePath.html
我添加了角数组长度的调试输出。我得到了0。
// ShowGoldenPath
using UnityEngine;
using UnityEngine.AI;
public class ShowGoldenPath : MonoBehaviour
{
public Transform target;
private NavMeshPath path;
private float elapsed = 0.0f;
void Start()
{
path = new NavMeshPath();
elapsed = 0.0f;
}
void Update()
{
// Update the way to the goal every second.
elapsed += Time.deltaTime;
if (elapsed > 1.0f)
{
elapsed -= 1.0f;
NavMesh.CalculatePath(transform.position, target.position, NavMesh.AllAreas, path);
Debug.Log("ShowGoldenPath corner count: " + path.corners.Length);
}
for (int i = 0; i < path.corners.Length - 1; i++)
Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.red);
}
}我在地面上创建了一个NavMesh,并将MonoBehavior脚本放在播放器虚拟对象上。然后,我创建了一个球体,并将其分配给脚本的target属性。实际上,目标是可以通过NavMesh到达的,但不能直接前进。
为什么这不给我一条非零弯道?
发布于 2022-11-07 19:53:09
我通过在统一编辑器中增加NavAgent的高度来解决这个问题。
对调试器中的方法进行跟踪表明,CalculatePath声称“导航路径无效”。这个方法返回了“false`”。
解决步骤:
打开导航选项卡。在代理卡中,您可以通过设置名称、半径、高度和高度来添加/编辑代理类型。增加heigt,直到CalculatePath方法可以找到代理的位置。
https://stackoverflow.com/questions/74351033
复制相似问题