My player character keeps appearing on top of all nodes no matter the Z-index

Godot Version

4.5.stable

Question

Just as the title says, the player character just always appears on top. It looks like whatever I do changes for the editor but once I try to play it the player always is above everything. What am i doing wrong here?

nodes are rendered in the order they appear in the tree. you should use nodes to set a minimum of 3 layers for 2D:

background
middle
	|-> player	
foreground

the player is usually placed in the middle layer.

for top-down games you should use Y-Sorting, which is designed to simulate depth, so when the player is below a tree the tree will be behind, but if they are above, the tree will appear in front.

something could be changing when the game runs, like from a script.
on top of the tree there’s a button called Remote, pressing it will show you the tree of the game running. there you can look at where the player is in relation to the other nodes.

also, some people will recommend using CanvasLayers, but it’s not a good idea unless absolutely necesary, you can use only node2Ds and tilemapLayers for everything. CanvasLayers are for special situations and maybe for UI.

It sounds like your player character is nested under a Canvas Layer node

My node structure looks like this
node 2d
|_Odering node(node2d)
…|_player(characterbody2d) y sort is enabled with z offset at 1
…|_turret(staticbody2d) y sort is enabled with z offset at 1

I am trying to use the y sort but it seems to only apply in the editor, its displaying that once it is high enough the player will appear behind another object but will not apply during the actual runtime of the game

Can you upload your source code to github.com and share the repo with us? It’s almost impossible to help you with the lack of context.

I have never uploaded anything to github and i also dont know what youre wanting me to upload. Are you wanting my whole file system? or just my scripts, because i didnt think that my y sort and z index would be a part of my scripts since i was using the editor

I figured out a fix for this, my sprite for the player was set to z as relative which for some reason applied as an always on top. I turned that off and the y sort properly worked after that.

1 Like