• Godot HelpGodot 4.X
  • Y-sorting a scene with multiple parts that should have different Y sort origin

What I'm trying to do:

I'm completely new to Godot (4), and I'm prototyping a top-down 2D game. I have a separate Tree scene with different parts that can be set in the Inspector, such as Roots, Trunk, different levels of Branches and so on. I want to give these parts different fake "heights" in the game, so that the Roots are Y-sorted with a different origin than the Branches in the crown of the tree.

For tiles in a Tileset, I figured out Y-sorting to reasonable satisfaction, because I could set the Y sort origin under Layers in the Inspector. However, this option does not seem to be available for just any Node2D or sprite. And in any case, I want to be able to build large trees with branches at different heights, that should have their Y sort origin at different points and preferably controllable by script.

What's happening:

In the screenshot below, you can see how my Grass Blades are Y-sorted correctly with respect to the Player, thanks to them being part of a Tileset. For the Tree however, something unexpected (at least to me) is happening. The Roots and the Trunk are being drawn below the Grass Blades (and the Player), but the Branches are actually being Y-sorted! You can walk behind the branches, and they are partially being drawn below the Grass Blades, which is a good start.

However, the cutoff for in front and behind is also wrong. You can see this with how the the Grass Blades are partially drawn over the orange branches - the cutoff appears to be just about at the top of the Trunk (although this doesn't change if I scale or move the Trunk sprite).

Further information:

I have the Tree scene organized as a Node2D with Y-sorting enabled, and everything else as children of that Node2D. I've tried manually enabling Y-sorting for the children, but that seems to have no effect whatsoever, so I'm leaving it off for now.

Any thoughts or pointers would be greatly appreciated! I'm not necessarily married to this way of creating the tree Scene, but I do want to avoid making it a Tileset if possible.

  • xyz replied to this.

    mirshafie Try to move the tree graphics inside the tree scene up so that its roots are near the origin.

      xyz Yeah, I've been moving it around both with Transform and Offset without actually achieving anything.

      • xyz replied to this.

        mirshafie In Node2D is a the of your scene, you need to move its children so that Node2D's origin is positioned where Tree's roots are.

        EDIT: Rephrasing: If a Node2D is top node in your scene, you need to move its children sprite(s) so that Node2D's origin coincides with vegetation's foundation.

          xyz Yes, looks like the problem was that I had placed my Tree scene in a different Node2D inside the World. I thought this would have been a good way to organize scenes, but apparently this is not the proper workflow for Godot. Thanks for your assistance!

          • xyz replied to this.

            mirshafie Lol, my previous post looks like some kind of semi-nonsense due to multiple typos 🙂
            So have you solved it?
            The gist is; sorting is done by the y position of node's (or scene's) origin point. So if you have vegetation items of varying height, and each such vegetation item is a scene, those scenes need to be edited in such a way that the part of the plant that is attached to the ground coincides with the origin of the plant scene's top node.