- Home /
How do I Find the distance from A NavMesh to a destination using NavMesh.CalculatePath
I am trying to make my Enemy character calculate the distance to a way point and if it can complete a path. The problem is that it doesn't seem like a path is being calculated. When I set the way point the character moves there no problem, but I can't get the distance to it. In the attached photo you can see that the debug PathComplete is not there, and the path length is 0. Any help is greatly appreciated, thank you for your time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WayPoint : MonoBehaviour
{
Vector3 pos;
Vector3 target;
float pathLength;
NavMeshPath path;
public GameObject npc;
void Start()
{
}
void Update()
{
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "WayPoint")
{
Debug.Log("WayPointFound");
pos = npc.transform.position;
target = other.transform.position;
path = new NavMeshPath();
pathLength = 0.0f;
NavMesh.CalculatePath(pos, target, NavMesh.AllAreas, path);
if(path.status == NavMeshPathStatus.PathComplete)
{
Debug.Log("PathComplete");
for(int i = 1; i < path.corners.Length; i++)
{
pathLength += Vector3.Distance(path.corners[i - 1], path.corners[i]);
}
}
Debug.Log("FinalPathLength" + pathLength);
}
}
}![alt text][1]
[1]: /storage/temp/164928-unity-help.jpg
Debug.Log the path.status; If it is partial, check how close the last corner is to the target destination. $$anonymous$$aybe it doesn't exactly reaches the target but just looks like it does when you set the path.
I did this and got PathInvalid. Why could this be, if I set the same waypoint the character walks right to it.
Answer by HumpaJac000 · Aug 07, 2020 at 09:50 PM
Solved it, The Enemy has obstacles on it preventing the path from being completed. *Face Palm
Your answer
![](https://koobas.hobune.stream/wayback/20220613005107im_/https://answers.unity.com/themes/thub/images/avi.jpg)