HOW TO DO Y-SORTING IN GODOT 4.4
1. Basic Node Structure
Make sure your scene follows this structure:
YSort
└── Character (e.g., Node2D or CharacterBody2D)
└── Sprite2D
└── CollisionShape2D
IMPORTANT: The node that moves (the character) must be a direct child of the YSort
node — not a grandchild.
2. Enable Y-sorting
-
Select the
YSort
node. -
In the Inspector, ensure the property
sort_enabled
is true (enabled by default). -
Godot will automatically sort all children of the
YSort
node based on their Y position.
3. Keep the Sprite Centered
Make sure your Sprite2D
is centered inside the character node, because Y-sorting works based on the parent node’s global position, not the sprite’s offset.
4. Beware of Z-index Conflicts
Godot 4.4 still uses Z-index (under CanvasItem
), and it can override Y-sort behavior if not set correctly.
On your Sprite2D
:
-
Set
Z-index
to0
or leave it empty. -
Uncheck
Z as Relative
.
This ensures the YSort logic takes priority.
What if you have multiple characters?
Easy — just make all of them children of the same YSort
node:
YSort
├── Player
├── Tree
├── Enemy
💡 BONUS: Instancing at Runtime
If you spawn characters or enemies at runtime, as long as they are children of the YSort
node, they'll be sorted properly without any extra setup.
🔧 SMALL CODE TIP (Only if you want to force the order manually)
Normally not needed with YSort
, but you can still manually change z_index
:
$Sprite2D.z_index = 1 # Force to render on top (not recommended with YSort)