Sorting sprites for render in 2D

Hello! I have a problem concerning rendering orders in 2D.

The problem can be seen in the following images: I have my character at a sorting layer above the tree-sprite. When the character walks behind the tree I need to swap the sorting layers somehow, anyone got any idea how to do this in unity?

30673-tree2.png Image 1. No problem with sorting here, tree is one sorting layer behind my char

30674-tree1.png

You don’t need to change sorting layer. You need to swap your tree and character sprites sorting order.
Unity - Scripting API: Renderer.sortingOrder .
Sprite with bigger sorting order will be rendered in front of sprite with smaller sorting order.

you can put the tree and the character in a same layer and change the z value when evere you want to have the character behind or not.

Also if you don’t want your player to walk through the base of the tree, just add a small box collider at the base. This would mean that you can still walk behind the tree but not through the base of the tree.