Godot Version
Godot Engine v4.5.1.stable.steam [f62fdbde1]
Question
I have a top-down 2D strategy game that is using AnimatedSprites and TilemapLayers. I’m having trouble working out how to mask Tiles when a Unit is standing behind them, and was hoping anyone might be able to push me in the right direction.
Here I have a bunch of units standing in a forest. The Units are on layer 0, and so is the Tree TilemapLayer. Both have y-sorting enabled because I think it looks good, and the Tree tiles have a size of 1x2 in the atlas, so they are functionally one tile on the tilemap.
The problem arises when a unit stands behind them. They get obscured and it’s hard to see which unit it is without panning the cursor over them. If you’ve made a 2D game this is a very common problem, but since the tiles are in a TilemapLayer, and since they’re y-sorted, this has become a bit of an issue for me. The easiest and most practical solution is to simply have all tiles that a unit can stand behind be stumps - like the one below the red zombie on the right, but these tree’s are destructible, so a player could theoretically burrow into the forest and still have this problem. I could also simply discard the muti-tile-sized-tiles, but I like how it looks with the tall trees as opposed to 1x1 sized trees.
Additionally, some units can fly, and stand on top of objects that are normally walls, so a scenario like this is possible: Where the unit is sorted to the front (thanks to y sorting) and you can still see the tree behind them. This is currently functional and doesn’t need any changes to make work.
How I’d like everything to work is something like this. Where the tile is masked out, and the unit is visible, but only when they’re behind the tile.
So the solution needs to fit three conditions:
- Tiles and units still have y-sorting enabled on them, so that the forest still looks cool and so units can stand in a line vertically and still look good
- Units can stand behind a 1-by-2 or X-by-Y sized tile and the tile is masked
- But if the Unit’s position is actually on the Tile’s origin the Unit stands on top of it
I’ve tried a couple of things:
- I followed this tutorial here that utilized the BackBufferCopy node to mask out the Tilemap with an image. This didn’t work for two reasons:
- All of these assets are on the same layer (because of y-sorting), so the masking-object ends up masking away the Unit asset as well
- It ends up masking the tile, regardless of if the unit is standing behind them or in front of them. This could in theory be solved with code that detects if the tile you’re standing on is the origin, and then only enabling the masking image if it isn’t.
- I could let the Units stand on Layer 1 and the Tilemap stand on Layer 0 and then the mask works, but I’d still be masking out the Tile behind them if they’re flying. And abruptly turning off the mask in those scenarios feels clunky and jarring. It’s not a clean solution.
- I tried changing the modulate of the tile using the get_cell_tile_data() method when a unit is behind them but:
- That changes the modulate of every instance of that tile on the map and
- That changes the transparency of the whole tile, not just the top of the tile like I want
- I could painstakingly have the tops of the tiles on an entirely separate tilemap layer and then when the tree is destroyed, delete the tile on that extra layer - but again it feels clunky.
- I’ve investigated a couple of other shader options but came up empty-handed.
- In Unity I’d use a multipass shader (I believe that’s the right term?) to write any tilemap on a visibility layer to an image, then take any unit on another visibility layer and stencil around them if they overlapped, then write that to an image to be rendered to the camera. I feel like the BackBufferCopy is sort of doing that, but if it’s capable of doing what I want, I’m not sure how.
- Godot has support for 3D stencil buffers, but afaik I can’t utilize that in 2D. Though if I’m wrong, that could be the immediate and best solution, as all I need is to be able to see the silhouette of the unit so I know what it is.
Any advice of potential solutions that I can investigate would be greatly appreciated! I’m getting the sense that I could be SOL and might have to compromise on either the visuals of the Tiles, or the readability of the Units, but I’m hoping someone might know of a silver bullet that can get me exactly what I’m looking for. Thanks!






