- Home /
How to know if a NavigationMeshAgent is impossible to reach Destination
I’m wondering to know if there is some way to get TRUE or FALSE, if the NavigationMeshAgent will never reach the destination point.
(I want to know it from the beggining, not when the object velocity is 0,0,0)
(c# if is possible)
Thanks a lot if someone knows.
Answer by YoungDeveloper · May 10, 2016 at 02:11 PM
I believe CalculatePath http://docs.unity3d.com/ScriptReference/NavMeshAgent.CalculatePath.html retuns bool which represent if the path can be calculated - exists. You can't be sure about dynamic obstacles though.
Answer by $$anonymous$$ · May 11, 2016 at 02:20 PM
This works fine:
if (this.transform.GetComponent<NavMeshAgent> ().pathPending) {
Debug.Log ("False");
} else {
Debug.Log ("True");
}
This does not answer the question and pathPending will often be true during the first few frames after calling SetDestination. See Ta$$anonymous$$g the Unity Navigation System for more details.
Answer by insominx · May 11, 2016 at 03:49 PM
As mentioned by YoungDeveloper, CalculatePath will give you a result immediately after calling it with the information you're after. Here's some sample code from the documentation:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
private NavMeshAgent agent;
void Start() {
agent = GetComponent<NavMeshAgent>();
NavMeshPath path = new NavMeshPath();
agent.CalculatePath(target.position, path);
if (path.status == NavMeshPathStatus.PathPartial) {
}
}
}
However if you're using SetDestination, you may have to wait a few frames for an answer and the method will be similar. Like before, you check the path status only with the exception that you have to wait for pathPending to be false (otherwise Unity may still be calculating it).
See here for more details: Taming the Pathfinding System
Thank you for help, as i'm sure you know more than me Iv'e changed my comment as not valid answer.
Even though, your comment does not help me as I wish. Don't understand how can I use this code sample.
If you're using CalculatePath then the return value will tell you what you want to know. True if there is a path and false if there isn't. I include it for people using SetDestination who will need to check the value of status and pathPending in order to know.
Your answer
Follow this Question
Related Questions
Navmesh Agent "move back" when player comes to close? 1 Answer
NavMeshPath.corners.length is always 0 1 Answer
Make a flying enemy ? 0 Answers
Navmesh agent forward movement 1 Answer
How to make NavMeshAgents traverse two possible paths 1 Answer