Quick answer: Since Godot 4.3 each layer is its own TileMapLayer node, sorted like any other 2D node: by z_index, then scene tree order. For walls the player can walk in front of and behind, put the layers and the player under one parent with Y Sort Enabled, turn on y-sort on the wall layer, and tune each tile's Y Sort Origin.
Ground, decoration, walls and characters all have to agree on who is drawn on top. Godot gives you three tools for it — z_index, tree order and y-sorting — and the problems come from mixing them without a plan.
TileMap vs TileMapLayer
Godot 4.3 deprecated the multi-layer TileMap node in favour of one TileMapLayer node per layer. In older projects, layers are configured in the TileMap's Layers list, each with its own Z Index and Y Sort settings. The rules below apply to both; with TileMapLayer you just set them on each node.
Rule 1: z_index first, then tree order
Layers with a higher z_index draw on top. With equal z_index, the node lower in the scene tree draws on top. A simple top-down setup:
Level (Node2D)
├── Ground (TileMapLayer, z_index 0)
├── Decor (TileMapLayer, z_index 0)
└── Player (CharacterBody2D, z_index 0)
If the player is drawn under the decor, it is either above it in the tree or has a lower z_index.
Rule 2: per-tile z_index
Individual tiles can set their own Z Index in the TileSet editor (tile Rendering properties). A single tree trunk tile with z_index 1 will draw over the player even though its layer is below. Check this first when only certain tiles misbehave.
Rule 3: y-sort for things you walk behind
For walls, trees and furniture that the player should pass both in front of and behind:
- Enable Y Sort Enabled on the common parent (the Level node).
- Put the wall TileMapLayer and the player as children of that parent, with the same z_index.
- Enable Y Sort Enabled on the wall TileMapLayer itself so its tiles sort individually.
- In the TileSet, set each tall tile's Y Sort Origin to where its base touches the ground.
Keep the ground layer at a lower z_index (for example -1) so it never sorts above characters.
Common mistakes
- Y-sorting the parent but not the TileMapLayer. The whole layer then sorts as one object by its origin, so walls pop in front of or behind the player all at once.
- The player's origin is not at its feet. Y-sort compares node origins. If the player's origin is at the sprite centre, it sorts too early. Move the sprite up so the origin sits at the feet.
- Different parents. Y-sorting only compares siblings under the same y-sorted parent. A player inside an Entities node that is not itself y-sorted will not interleave with wall tiles.
- CanvasLayers. A layer inside a CanvasLayer is always above or below the world, whatever its z_index.