1
\$\begingroup\$

I am trying to get coordinates for path from source to destination object using NavMesh. NavMesh.CalculatePath() is only working for one level surprisingly. For all others, it returns false. Also NavMeshPathStatus is invalid. This is working Level, while this is not working fine with navmesh.

Code:

private NavMeshPath path;

    void Start () {
        path = new NavMeshPath();
    }

//Following function is called on button press
public void giveHint() {

        NavMesh.CalculatePath(source.transform.position, destination.transform.position, NavMesh.AllAreas, path);


    if (path.status == NavMeshPathStatus.PathComplete)
        print ("Path complete");
    else if (path.status == NavMeshPathStatus.PathInvalid)
        print ("Path invalid");
    else if (path.status == NavMeshPathStatus.PathPartial)
        print ("Path partial");

    for (int i = 0; i < path.corners.Length-1; i++) {
            Debug.Log(path.corners[i]);
        }
   }

// Update is called once per frame
    void Update () {
    if (path.corners != null) {
            if(path.corners.Length > 0) {
                for (int i = 0; i < path.corners.Length-1; i++) {
                Debug.DrawLine (path.corners [i], path.corners [i + 1], Color.red);
                }
            }
        }

    }

Please tell me way to debug/solve this issue. How unity calculates path between source and destination? Can there be possibly blockage in path? If yes then how? How path is invalid? Is there any bottleneck with the usage of CalculatePath function? Need guide from you guys... Thanks!

\$\endgroup\$
12
  • \$\begingroup\$ Check what CalculatePath is returning. If it's false, then it was unable to find a path. \$\endgroup\$ – Foggzie Aug 8 '16 at 20:37
  • \$\begingroup\$ could you somehow display your navmesh? the one working and one which isn't? Or check the way you initialize the navmesh in other levels \$\endgroup\$ – Leggy7 Aug 8 '16 at 20:39
  • \$\begingroup\$ @GuntherFox It is returning false. Just checked :( \$\endgroup\$ – NightFury Aug 8 '16 at 20:41
  • \$\begingroup\$ I never used navmeshes but I assume that you somehow set up in the scene view and have properties in the inspector. maybe a print screen of that and also of the shape in the scene view could be of help \$\endgroup\$ – Leggy7 Aug 8 '16 at 20:44
  • \$\begingroup\$ @Lara If it's returning false then my guess is there is some obstacle or obstruction. \$\endgroup\$ – Foggzie Aug 8 '16 at 20:45
1
\$\begingroup\$

Finally, I figured out where I was wrong. I have a cube gameobject as destination. I was not making it part of navmesh baking. Navigation Static was not marked for it. So when navmesh was baked, destination was not included as part of it, so path status was invalid.

I cleared the navmesh, made my cube Navigation Static and Walkable, baked the navmesh again. Now I am getting valid path for all my levels.

enter image description here

Hope it helps someone!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.