0

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?

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42

1 Answers1

0

I managed the issue by increasing the NavAgent's height in the Unity editor.

Tracing the method in the debugger showed that CalculatePath claimed 'navmesh path invalid'. The method returned 'false`.

Steps to solve:
Open the Navigation tab. There in the Agents card you can add/edit your agent types by setting the name, radius, height aso. Increase the heigt until the CalculatePath method can find the location of your agent.

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42