Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | batmanasb | |
Old Version | Published before Godot 3 was released. |
I’m making a 2D RPG and need to sort all sprites in the scene by their Y axis so that things on the bottom overlap things on top. Is there an way to do this in Godot? I noticed some old forums recommend the YSort node but as you can see the documentation is kinda empty and doesn’t explain what it is. My guess is that it sorts the Z value of all it’s children based on their Y position, but I’m not sure.
Here’s the code I’m currently using:
#order the z layers based on height
func zOrdering():
#object's height = y coordinate - bottom of it's sprite
var characterBottom = bodySprite.get_item_rect().size[1] * 0.5
var heightOnCanvas = get_global_pos()[1] + characterBottom
set_z(int(heightOnCanvas))
It literally sets the Z value to the Y position of the bottom of the sprite, this way I can stand in front of trees and move behind them, all with having one sprite for each tree. I found this method from a Unity tutorial.
However, it doesn’t work so well in Godot because the maximum Z value seems to be 4096, so once my character goes too far away from the center of the scene, it starts getting errors.
Does anyone know a better way to Y sort in Godot, or how the YSort node works?