How to do this? (Layering?)

How do I make my character to be blocked by the tree when he is behind
and to block the tree when he is in front… something like this in the image. Also, how to do it so the tree becomes transparent when my player is behind it, something like on the 2nd image.

If you use sprites it’s fairly simple, sprite renderer contains rendering order, so, you can create a script that controlls depth of an object in rendering order.

How? First - be sure that your “pivot” (object supposed to control depth) is a the bottom of an object/sprite.

Second - create a script changing rendering order based on Y, the simplest solution is GetComponent<SpriteRenderer>.sortingOrder = -transform.position.y; which makes object lower (e.g. theoretically closer) render on top of those further away. The better way is to create a list of objects and sort them via one script, this way sorting order is cleaner and more accurate, but at the same time it won’t be well optimized for multiple objects.

Third - static and dynamic objects. With whichever method you go you should call this function once per static objects and regullary per objects that move e.g. change their Y position.

That’s all about sprite depth with my solution, but for “ghost sprites” I would use Camera.WorldToScreenPoint(character's position); (might be wrong about the name, but function of this kind exists) and cast ray there, so if it hits an objects you change the alpha color of it, but this part is just a speculation, not sure about optimization etc.

Also if you want to increase the accuracy of first method you can multiply -transform.position.y * number;, the bigger number, the more accurate.