Godot Version
4.7.2.stable
Question
I have some agents that i want to randomly roam around the tilemap. The navigation seems to work but it’s not properly accounting for the physics collisions of the agents. The path is no good for turning corners etc.
Tilemaps do not seem to have any sort of avoidance layer system i can make use of, how can i get agents to avoid physics collisions in these cases?
A picture to help explain:
The more i ponder this problem, the more it makes me wonder if this is a feature request. After all, for agents with differing collision shapes, their navigation margin needs to differ. I don’t see a way of setting agent radius for the tilemap nav layer so this might just be a feature needed for tilemaps.
I’ve been wrestling with a similar problem and here’s what I’ve found
- The Navigation system only tells agents where they can walk theoretically - if you want agents to path around an obstacle then you have to remove the area of that obstacle from the navigation region.
- One way to do this is to have NavigationObstacles with the “Affect Navigation Mesh” option enabled as a child node of the NavigationRegion - in your case the navigation region is calculated from the combination of the TileMapLayer navigation so they should be a child of that.
- If that doesn’t work, an easy way I’ve found is to have a giant NavigationRegion at the top level of the scene. This works nicely because NavigationPolygons have an option to scan their children for StaticBody2D’s and have them automatically removed from the area, so any static obstacles with CollisionShapes will be automatically excluded from navigation. If StaticBody2D’s aren’t an option, you can still use NavigationObstacles.
- If you give the node with the NavigationAgent a CollisionShape, they will still engage with physics objects like normal and they can stray a certain distance from the path while being pushed by an object ← works better with capsule shapes for some reason
- The avoidance system is more for allowing agents to move around each other while following their path; it shouldn’t really be thought of as a way to affect the pathfinding itself. You can also use it for making agents move around small objects on the floor, for example.
I hope some of that helps anyway lol, it’s a pretty confusing area and the documentation is a bit thin.
I’ve found that i had to actually just do away with the nav layer on the tilemap and manually create an AStarGrid instead. No need for navigation agents. My NPCs will now travel using paths that are centered on tiles.
The problem with navigation is that it treats all agents as a point traversing the nav region that the tilemap creates, with no regard for the size of the agent. In order to make agents avoid getting stuck on corners i’d have to do some hacky stuff with manually adding margins on the nav regions for each tile.
If this were a nav region created in any other way it might be easier, it’s just that it’s not so simple when you’re dealing with a grid-based tilemap nav region.