Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

NavMeshAgent.SetPath always returns false

Discussion in 'Navigation' started by Shadowphax, Aug 11, 2015.

  1. Shadowphax


    Jul 26, 2013
    Hey guys

    The title pretty much says it. I have a list of paths calculated through the NavMesh class. When I debug I can see the paths are valid. I assign the first path in the list to a list of NavMeshAgents through the SetPath function. However, only the first assignment is successful. Successive assignments always return false (which the docs indicates means that the assignment has failed).

    What are some causes for this SetPath to fail? The path at this stage is already calculated and at the time of assignment the path is still valid and looks as expected.

    I set the path directly after instantiating the GameObject which contains the NavMeshAgent component if that is of any help.

    Here is some of my code:
    Code (csharp):
    2. // Assign the path
    3. public void OnUnitSpawned(GameObject _unit)
    4.     {
    5.        // Always false
    6.        // m_Path[0] has 6 corners and is complete.
    7.         bool success = _unit.GetComponent<NavMeshAgent>().SetPath(m_Paths[0]);
    8.         m_Units.Add (_unit);
    9.     }
    11. void Update()
    12.     {
    13.         m_LineRenderer.m_Show = m_DrawPath;
    15.         for(int i = m_Units.Count - 1; i >= 0; --i)
    16.         {
    17.            // Here I check if the unit has reached it's destination.
    18.             NavMeshAgent agent = m_Units.GetComponent<NavMeshAgent>();
    19.             if(agent.remainingDistance == 0)
    20.             {
    21.                 for(int j = 0; j < m_Paths.Count; ++j)
    22.                 {
    23.                     if(agent.transform.position == m_Paths[j].corners[0])
    24.                         agent.SetPath(m_Paths[j]);
    25.                 }
    26.             }
    28.             // If it's still 0 then the unit is at the end of the final path.
    29.             if(agent.remainingDistance == 0)
    30.             {
    31.                 m_OnUnitReachedDestination.Invoke(m_Units);
    32.                 m_Units.Remove(m_Units);
    33.             }
    34.         }
    35.     }
    I've noticed that once the first agent finishes the first segment, the next path assignment on that agent also fails. So exactly one assignment succeeds ever.
    Last edited: Aug 11, 2015
  2. Shadowphax


    Jul 26, 2013
    Okay so the problem was that the NavMesh was stale at the moment that I called a recalculate. Seems there isn't a good way to determine if the NavMesh has changed since adding obstacles. I.e. I add obstacles and call CalculatePath() but then the NavMesh carving only happens after calculating the path.

    If there is a good way to check for NavMesh changes please let me now because right now I'm yielding for a second after a change. Luckily due to the gameplay I can get away with that this time but there has to be a better way.