I do not get the method NavMesh.CalculatePath
working, not even after copying the original example from the Unity documentation page. https://docs.unity3d.com/ScriptReference/AI.NavMesh.CalculatePath.html
I've added a debug output of the array length of corners. I get 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);
}
}
I've created a NavMesh
on a ground plane and placed the MonoBehavior
script above on a player dummy object. Then I've created a sphere and assigned it to the script's target
property. Actually, the target is reachable through the NavMesh, but not immediately straight forward.
Why doesn't this give me a non-zero corners path?