How to make a character sprite hidden behind a wall in a TileMap

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

Dear Godot community,

I am working on a 2D project where I have a player sprite that should be able to be in front of a wall and walk behind the wall. I am using wall tiles with a width of 16 and a height of 48 pixels. My TileMap grid is 16x16. The TileMap has the Y-Sort option checked.

The player sprite has an y-offset to set the origin between his feet.

As you can see on the first image, as long as the player’s origin is above the wall tile’s top, it is drawn behind the wall. If I move it down, it is drawn in front which is wrong, since the player should still be behind the wall.

I fixed that by setting the tile’s texture offset to -48, like you can see on the next two images:


But I have the impression that this is the wrong way to do it, since I need to set the offset as well for collisions and occluders for each tile. Otherwise they are in the wrong place.

wrong placed collision

I would like to know if this is the correct way to do that, or is there a solution which works more out of the box.

Thanks and cheers.

:bust_in_silhouette: Reply From: 3Dski

I know this is an old question, so someone else may have responded to you.

First, set the z-index of the column to the y position of the offset. Second, in your movement script set the player’s z-index to its y position. in doing this the draw order will be changed based on the player position in relation to the column’s position.

For collision… You will have to reposition your collision shapes since it looks like you probably create these before you changed the offset on the objects. I’m personally trying to figure out the collision part of the scripting now.

This is so useful!

TheNewGrant | 2020-06-18 22:00

:bust_in_silhouette: Reply From: dopey_kun

I know this is old, but I’m answering for anyone searching in the future.

In the inspector, set the Tile Map’s Tile Origin to Bottom Left. It’s not perfect, but it works for y-sorting.