2D rpg Trees and layers

I tried a lot but I couldn’t make a tree that the player can pass in front of it, behind it and also collide with it in the right place.

I’m working on 2D-rpg game (snes camera perspective).

I was able to make the player appear in front and behind the tree correctly using this code:

 transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.y/100);

I used it on the start of the tree and in the update of the player, so the tree would have its own Z and the player would appear in front of and behind the tree depending on the y position.

But changing this z position killed me to set a collision between them, so how can I do it?

PS: if there is another approach, it could be fine too, as long as it works :slight_smile:

I’m guessing top-down, like Zelda? SNES games don’t use only one perspective!

Setting z relative to the y-position will work, but using layer order (seen here) to change draw order is the preferred method. You can do that based on the y-position as well.

If you’re using 2D physics, though, z-positions are discarded before performing physics calculations, so the z-position shouldn’t affect collision. Are you using 3D physics by accident?

1 Like