0 votes


I have a Tileset which contain many elements that should be drawn over the player Sprite. To do that, I changed the value of the Sprite of each Tile concerned, and when I use the converted Tileset.tres in a Tilemap, my player is still drawn after thoses tiles.

However, when I set the Z value of the Tilemap, I can correctly draw the entire layer on top of the player Sprite.
Does setting Z value for individual Tiles in Tileset is not supported, or am I doing something wrong ?

I'd appreciate any help!

in Engine by (17 points)

2 Answers

0 votes
Best answer

Not that I know of.
The best option may be to create a secondary TileMap for foreground objects (like one normally do with other engines/tilemap editors).

by (7,898 points)
selected by

To be clear -- since I couldn't figure this out without some experimentation -- the process is:

  • Create two tilesets (say ground and objects)
  • Create two layers: say one grass/ground and one trees/objects
  • Set the Z-index on the object layer to a higher value. Setting it on the tileset (eg. root `Node2D element), or on individual tiles, doesn't do anything.

I was just about to do that, but my sprites are all z-indexed by their Y position, so some will be behind a wall while others will be in front of the wall. This solution won't work for that.

Here is what I ended up doing in my 2d top down for things like tree's, where you are z-indexing by y like paul.holt above:

  • Update player object z-index by its y position at the end of its _PhysicsProcess method.
  • Use a TileMap for the ground under the player.
  • Make your "foreground objects" into Sprites (Things like trees for example)
  • Make a script that extends Sprite, and in _Ready update its z-index to its y position
  • Make your sprite a child scene and instantiate it on your main scene everywhere you want it

That works really well for me and my game, and is easy to implement.

0 votes

Maybe it was not available yet when this question was first answered, but to be clear, you can change the z_index of individual tiles in an atlas or autotile. There is a tab for z_index on the same menu where you can select a region of tiles, set bitmasks, collisions, etc for a tilemap. You can set the bottom tiles of an object to have a different z_index for example.

However, if you are trying to have a player be able to go behind and in front of objects in top down 2D world, it may be better to go with a solution like LuminousSilver mentioned. I would recommend creating each object as a sprite and make all the sprites the child of some node. Add all the sprites to a group. Then you can attach a script to the parent node to call all the nodes in the group and set the z_index equal to their y position. For example, I put all my building sprites in the group "Buildings". I add the following code:
To the player:

z_index = position.y

To the parent node of the building sprites:

func _ready():
    var nodes = get_tree().get_nodes_in_group("Buildings")
    for node in nodes:
        node.z_index = node.position.y
by (38 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 webmaster@godotengine.org with your username.