I want to do pathfinding in 2D using several regions that have overlaps, each region being set on a different layer. Even on a simple test scene I get weird behavior, I don’t know if it is a bug or a feature
I have two navigation regions (each with one simple polygon), the two polygons are overlapping and each region is set on a different navigation layer. My navigation agent uses both layers.
So I was expecting my agent to be able to path find using both regions if necessary. Here if my agent is in the first polygon / region and I set a target that is within the second polygon/region, it won’t be able to find a path using both regions (see below, target is at the bottom right, the debug path shows it does not compute a path using both regions)
If I set first an intermediary point in the overlapping region, I get inconsistent behavior : sometimes it is able to transition to the second region, sometimes not, so it is not a reliable workaround.
Is there a way to do this reliably ? I want to keep the navigation regions separate and on separate layers because I want to activate them at runtime based on game events.
They should connect at runtime too, I remember I had something like this working, but I can’t remember if it’ll work out of the box or there’s something else to it…
I understood that for different polygons in the same region, and it works as expected.
But here I’m trying to have different NavigationRegion each assigned to a different navigation layer, I don’t want different polygons in the same region.
The use case is the following : an agent which can “discover” a new part of the map, and hence get access to the corresponding navmesh. But I don’t want to give it the full navmesh before it discovers the new area, to prevent it to take shortcuts it’s not supposed to know.
So my strategy is to add more and more layers (and hence navigation regions) to that agent as it discovers more and more parts of the map.
Do you see a way to achieve this ? And why does it work “sometimes” ?
Navigation mesh can’t be layered on the same navigation map. If you overlap navmesh surfaces that is a logical error that breaks the pathfinding. It will also cause navigation map sync and merge errors.
The navigation_layers bitmask is a polygon filter for the pathfinding.
Imagine a terrain (in green) with a house (in grey) having two doors. If my agent (blue) is outside and has to go on the opposite side of the terrain (red cross), I don’t want it to go through the building (even if it’s shorter). So the navmesh has to cover only the outside.
But if certain game event happen, the building might become “open” to the agent, so I want to “add” the navmesh of the building. I could in principle have two different navmesh with/without the building, but with a lot of buildings that all can be accessible or not, it becomes untractable.
Any solution to do this ? Create and bake the polygon at runtime everytime a new building becomes accessible ?
The solution is to remove the navmesh from your main navmesh below your “house” and add a differen region with navmesh in place for each house. This way you can toggle the house region, change the cost, or the navigation_layer polygon filter for the agents.
The simplest way to do this is likely to just block the house shape with an obstruction for the baking of the main region navmesh and than use that same shape information to bake navmesh for the house region.
If there are seams between the navmeshes to connect you can use navigation links. No seams is always better but that will likely require you to fix or create parts of the navmesh procedual depending on how complex your shapes are.
Should I create one navigation region for the outside and one for each building ? Or one containing all the buildings but not the outside ? If yes how can I make sure that my agent can find its way inside of a building (since it can pathfind using several regions ?)
One region for the outside, and one for the buildings, and then navigation links at each door. At the beginning, those links do not belong to the navigation layer of the agents, but then if agents acquire the ability to navigate a building, I just have to add the links of that building to their navigation layer.
(I believe I could even do only one region with disjoint polygons and connect them with links at the doors)