Best way to handle Y-Sort on top down 2d game? Can't make player a child of Tilemap

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

Hi! So I’m making a 2d top down game and I have an issue with walls.

I need to make it so if the player is standing ‘in front’ of the wall he will be rendered on top, and if player is ‘behind’ the wall he will be rendered below.

I know there’s a way by making the player a child of the Tilemap and setting Y-Sort to true, but this would break the structure of my game. I will be instancing different tilemaps on runtime and I can’t have the player as a child to all of them…

How would you go about making this? Any ideas?

Of course what I DON’T want to happen is what you see on the picture below.

Thanks for your time!

Try using YSort Node and make Player and walls TileMap children of it. Not sure if will work.

pospathos | 2018-12-04 11:25

I just tried!

Sadly it doesn’t work, the Y-Sort just compares the TileMap position (not it’s children) to the player’s position. So all the walls can be either in front or below the player, it does not check it on a wall-to-wall basis.

Thanks for your suggestion!

BauRawson | 2018-12-04 12:22

Then only solution seems to be to dynamically (at runtime) reparent Player node. Dont make Player by default child of any TileMap, instead parent it to current TileMap (find some method to parent it if you have many TileMaps in one scene - e.g. is Player inside of a rectangle of tile map, if you have only one TileMap per level then things are easy). See this post for reparenting at runtime:

pospathos | 2018-12-05 15:23

Yes, after searching and trying a few things this seems to be the best approach. I really don’t like it but for now I’ll just stick to whatever works as expected :slight_smile:

Thanks for your answer!

BauRawson | 2018-12-07 01:42