+2 votes

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.

Tilemap setup

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

Player setup

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.

Wrong draw order

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

fixed
Tile y offset

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.

in Engine by (16 points)
retagged by

2 Answers

0 votes

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.

by (14 points)

This is so useful!

0 votes

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.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.