• 2D
  • Best methods for displaying trees in front or behind the player?

I'm having some trouble with the Z-index. I'm still really new to Godot, though, so this could be a simple fix.

All I want to do is make sure the trees are drawn behind the character if they're standing in front of it, and above them if they're standing behind it...

I have not really used it myself, but I think the Y-Sort node can achieve this effect. If I understand how it works correctly, the Y-sort node should make it where sprites and objects with a higher Y value are drawn behind those with a lower Y value, allowing the character to walk behind the tree.

@TwistedTwigleg said: I have not really used it myself, but I think the Y-Sort node can achieve this effect. If I understand how it works correctly, the Y-sort node should make it where sprites and objects with a higher Y value are drawn behind those with a lower Y value, allowing the character to walk behind the tree.

I'll look into that and see what I can do.

You could use layers of tilemap nodes in combination with the Y-Sort node.

Create a tilemap node for each layer, one to be drawn behind and one to be drawn in front of the player. Then split up the process of drawing the tile map. For example the tree (I assume it is the double tile size in height) you could split as follows: - the lower part you draw at the layer behind the player - the upper part you draw at the other layer to appear in front of the player

In combination with the Y-Sort node, you then could specify when to draw which layer (tilemap node), by settings their Y-values.

@Z3R0PTR said: You could use layers of tilemap nodes in combination with the Y-Sort node.

I've accomplished almost the same thing by adding a second sprite to the trees for the higher layer. I just set that sprite's z-index one higher, and boom.

I was just hoping there would be an easier way of handling it. The Ysort nodes kinda confuse me because wouldn't my Player need to be a child of the same Ysort node as the environment in order for it to sort everything correctly?

In case of using the Y-Sort node I think so. I've never worked with the Y-Sort node.

A solution could be to set the player node as a child of the layer (tile map) which should appear in front of the player an set the tile maps property "Y Sort" to true.

If yor problem is related only to the three things (lower layer, player, upper layer) you can try to handle this by changing the position inside your scene tree, so anything will be rendered in the correct order withou using the Y-Sort node by placing the player node between the to layers.

Anything I mentioned here is pure theoretical and could lead to other problems.

You could search on YT for "Y-Sort" and especially look for the video from Game Development Center. I remember, some time ago there has been posted a video for these kind of problem, but for isometric tilemaps. Maybe it helps though.

The problem when you use z-index is that you will experience similar problems with your enemies and bullets.

Here is a video tutorial that addresses this issue and makes use of y-sort.

You can also use the y-sort function on the tilemap itself, works similarly when it comes to placing and off-setting the texture of the tile to get the right y-order.

2 years later

This apparently worked...when I clicked on the tilemap and made a sprite using "+ Add Child Node". If I made a sprite then dragged it to be under the tilemap. This wasn't working, and doesn't seem like anything I do actually makes this work. if the sprite isn't made as a child node of the tilemap I'm wanting it to sort with....Which seems like a bug?