I want the object to be behind the avatar if the avatar’s Y position is greater than the object’s Y position. It works but only with the object with the greatest Y position; the other objects don’t work. I am aware on why it’s doing it but I don’t know how to fix it.
first, dont change everything’s Z order. you should use “static” objects like trash cans or vending machines, and “active” objects like the player sprite. I think the life will be esier. so, you set up all the static objects Z once when they spawn to the world, after that only the moving object needs to change it’s Z.
and thirdly If I want to create a game with similar perspective to this (I wanted it once), I would bring it closer to the 3D games. in 3D usually all objects origin above its foot, so they can adjust to the ground. but in a 2D game we hadn’t ground and the object’s origin is it’s top left corner. however, if we reposition all(!) object’s origin to it’s bottom center, I think we can use they “Y” coordinate as Z. for example in your screenshot: if player’s soles above the bottom of the trascan, hes “Y” is greater. and if Y=Z, his Z will be greater than the trashcan’s Z.
so i hope someone knows a mathematical solution to your problem, because set up all origin will mess up your project.
That perspecive is called 3/4 perspective and is the most used in classic RPG games of the 16bit videogame era.
3 tips for the type of perspective you are using in your game:
Set the origin point of each object at the base. I mean, at the place where it touches ground. For example, in the case of the character, set the origin point at their feet.
Then make the Z position of each object equal to its Y position value (this can be done once at scene start for the static objects and continously for the moving objects, like the player).
Make the collisions masks of the objects to cover just the theoretical three-dimensional base of the object. In the following picture, the green zones shows the collisions masks of each object:
In this way the objects will always overlap consistently with the perspective.
Here is a simple example of a character moving in 3/4 perspective and passing in front and behind objects: