Y-Sorting is not working with enemy while walking

Godot Version

4.5

Question

I have player, enemy and object instances (in that order) as children of a Node2D with Z Index = 1 (background has 0), Z as Relative and Y-Sorting enabled. The objects have collision thanks to a physics layer while the player and enemy use CollisionShape2D set on their feet.

The Y-Sorting with the objects works perfectly, but whenever my player makes contact with the enemy the enemy sprite overlaps the player’s, and when they’re idle the sprite order is inconsistent.

image

How did you set your sprites for enemy and player? Their sprites should be placed with an offset that their most bottom (feet) should be around local origin (0.0)


They’re set like in the left, I tried using the offset to set it like in the right but it made things worse (I couldn’t move above the enemy).

Can you share your node tree as a screenshot?
Maybe you should offset the immediate children of the ‘y offset enabled’ Node2D rather than the sprites.

It also helps if you share a video of the undesired behaviour.

My node tree is:

Node 2D (overworld)
→ TileMapLayer (grass and road)
→ Node2D (grants Z-Index +1)
→ -> CharacterBody2D (player)
→ -> CharacterBody2D (enemy)
→ -> TileMapLayer (trees, rocks, etc.)

Since I’m a new user I can’t show the video directly, so here’s a link to it:

Please check these:

  • Node2D should have y-sorting enabled.

  • The nodes below all should be on the same z-index.
    CharacterBody2D (player)
    CharacterBody2D (enemy)
    TileMapLayer (trees, rocks, etc.

  • The nodes below should probably not have y-sorting enabled. I say probably because I don’t know exactly how the child nodes are set there.
    CharacterBody2D (player)
    CharacterBody2D (enemy)
    TileMapLayer (trees, rocks, etc.

  • Make sure the nodes below are offset to make bottom (feet) of the characters are on 0,0. Not the children sprite2D nodes of them. Because if you enable y sorting on a node, it checks the position of the immediate child nodes, not grandchildren nodes.
    CharacterBody2D (player)
    CharacterBody2D (enemy)

Sorry for the wait, here are my nodes:

As for what you asked:

  • The overworld Node2D does have Y-Sorting enabled, and so does the internal one.
  • Those 3 nodes are in the same Z-Index, since they’re children of the internal Node2D.
  • I tried disabling Y-Sorting on those 3 children and that didn’t work.
  • I adjusted the offset again and the enemy is no longer alternating its order with the player, at least.

Have you checked this tutorial?

I adjusted the collision and texture origin on the trees and rocks TileMapLayer as the video said, now the player is behind everything, and when I increase the player’s Z-Index by 1 he goes above everything.

Can you share the node tree (including full player scene, not just the main node)?