Search Unity

Bug SetPath does nothing

Discussion in 'Navigation' started by Guedez, Aug 28, 2021.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    So this block of code does nothing at all because SetPath don't work:

    Code (CSharp):
    1.             NavMeshPath NavMeshPath = new NavMeshPath();
    2.             if (!NPCBrain.Agent.CalculatePath(result[current].pos, NavMeshPath)) {
    3.                 Debug.Log("Path not found from: " + NPCBrain.transform.position + " to " + result[current].pos);
    4.                 NavMeshPath = new NavMeshPath();
    5.                 var data = GameObject.FindObjectOfType<NavMeshSurface>().navMeshData;
    6.                 NavMesh.RemoveAllNavMeshData();
    7.                 var res = NavMesh.AddNavMeshData(data);
    8.                 if (!NavMesh.SamplePosition(NPCBrain.transform.position, out NavMeshHit hit1, 10, -1) |
    9.                    !NavMesh.SamplePosition(result[current].pos, out NavMeshHit hit2, 10, -1)) {
    10.                     Debug.Log("Could not sample to objective");
    11.                 } else {
    12.                     if (!NavMesh.CalculatePath(hit2.position, hit1.position, -1, NavMeshPath)) {
    13.                         Debug.Log("Path not found Anyways");
    14.                         Abort = true;
    15.                     } else {
    16.                         Array.Reverse(NavMeshPath.corners);
    17.                     }
    18.                 }
    19.             }
    20.             bool setpath = NPCBrain.Agent.SetPath(NavMeshPath);//none of these work
    21.             NPCBrain.Agent.path = NavMeshPath;
    22.             NPCBrain.Agent.SetDestination(NavMeshPath.corners.Last());//besides this one that defats the purpose of everything above
    The idea is to try to path to the target, and if it does not work, path from target to me (sometimes work when the first didn't) to try to get my actors actually moving. Except
    SetPath
    don't work. It does set the path, but
    hasPath
    stays returning false and the agent don't move.
    Also, needing to re set the NavMesh data every frame is bothersome, it somehow just don't work otherwise.