How to do pathfinding with collision on top of a tilemap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nailu

I’m new to game development and this might be a very basic question but I couldn’t figure out how to pathfind around objects I’ve searched for a few hours but the tutorials I found just use a single TileMap.
I’m trying to do a 2d RTS game and currently have a TileMap as well as units that I can move around using Navitagion2D#get_simple_path(). The tiles have NavigationPolygonInstances and the units are able to pathfind around them. I also use move_and_collide() and a CollisionShape2D per unit so they bump off each other (not perfect but at least they don’t walk into each other).
Now I’m having issues with spawning static objects like trees or buildings and having the units pathfind around these. I tried adding them as tiles but then a unit can’t walk behind a tree. I.e. they should only collide with the bottom of the tree and otherwise be able to walk around it. Since the TileMap is on the lowest layer, the sprites of units will be above the tree which looks odd. I also tried spawning these objects like I did with the moveable units but then the pathfinding won’t work, units just walk into static objects and slide off of those instead of pathfinding around. I’ve also tried adding another TileMap for objects as a second layer. But if the object TileMap is below the floor TileMap in the tree, then the collisions are ignored, if the object TileMap is above the floor the collisions work but the objects are not visible. It would be a very nice solution if I could simply make the second TileMap visible while the collisions work because then I could reuse the code for the first TileMap :slight_smile:
I’ve also tried adding a NavigationPolygonInstance to the Navigation2D which contains the TileMap but those polygons didn’t seem to have any effects…

:bust_in_silhouette: Reply From: Narudura

I know this one was raised in January, but im facing exact same problem.
Trying 2 tileMaps. One with navigation area and second with obstacles. I cant find a way to get the pathfinding to work with this. Not mentioning the fact that it seems like there is no support for multi layer tileMaps

I keep the tilemaps out of the Navigation2D entirely and create a simple (invisible) tilemap child to Navigation2D at runtime that combines collision and navigation objects from both tilemaps.

Seems like child tilemaps should pool that information under Navigation2D. That’s what the documentation entry for Navigation2D indicated would be the case.

arkiel | 2020-11-06 15:08