2D walking over/under objects

Hi all, in the first image you can see that my sprite walks over the top of the tree instead of under it, this is easy to fix however doing so with my method of changing the trees Z position causes the player to move underneath the trunk as shown in the second image.

How can I fix this so that the player can be seen while walking past the trunk but is hidden by the rest of the tree when walking behind it? thanks.


I assume you are trying to make some depth.

In your character script you should change the z position of the character while it gets closer or farther.

Also put some colliders on the tree root so your character don’t pass through it.

You have to use the sort order for the sprites instead of moving in z axis.