Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Issue using NavMeshPath.corners to render the path?

Discussion in 'Navigation' started by spiralogy, Jul 27, 2020.

  1. spiralogy


    Oct 10, 2013
    Bear with me here as this might be a little tough to explain: essentially, I have a tower defense game where there is one 'master' path through the area, which is being calculated every frame (or so) using CalculatePath. Essentially it just looks at the start position and the end position and creates a path between the two. I then feed the corners generated by this path into a line renderer to 'draw' it. Pretty basic stuff.

    Each mob, when spawned, will duplicate/instantiate the master path and take their own path through the area that they call a single time with SetDestination, again, only using the destination point, which is the same one used in CalculatePath from above. To be clear, they are positioned at the start of the path as well, so for all intents and purposes, they exactly follow the line renderer.

    So the next gameplay mechanic is to allow the player to place obstacles on the NavMesh, which then carve the mesh and therefore disallow mobs from walking where these obstacles are. Again, the mobs completely avoid carved areas and, since the main path updates every frame, the drawn path updates properly to navigate around carved areas, so the rendered path and the mob path are still 1:1.

    This seemed to work fine--until I noticed that it was possible to place NavMeshObstacles in positions that cause the line renderer to draw what are obviously incorrect paths. I figured, must've screwed up the renderer, OK. So I dig into it and I find out that the corners array actually is populated with 'false' points. These points always end up touching the edges of nav mesh obstacles, and are usually way out of the way of what should be the most efficient path--I'm talking straight zig-zags into hell--obviously incorrect.

    The thing is, the actual mobs that are using SetDestination always still take the correct path--they don't actually acknowledge these weird 'corner' points. Well, that's odd, something else must be happening. So, instead of rendering the 'master path' provided by CalculatePath, I decided to feed in the corners of the path as given by a random mob itself--so the line renderer is now rendering the 'corners' provided by the mob's SetDestination call--AKA, it's actually looking at the NavMeshAgent's path value.

    Lo and behold, the line renderer actually changes throughout the path of the mob--OK, well, wtf. It turns out that the corners array is actually changing dynamically as the mob moves through the scene. More precisely, it seems that the NavMeshAgent has additional logic to basically remove certain corner points as it approaches them, you know, say if they're literally mapped to somewhere inside Mordor. I get that the NavMeshAgent has to change its path to accommodate for nav mesh obstacles--however, I was not expecting it to actually do it when the agent gets close to the obstacle. This seems to imply that there is no way to get a known 'path' that doesn't include messed up corners because the corners get changed as the NavMeshAgent moves through the scene.

    TLDR: How do I get CalculatePath to behave more like SetDestination?