- Home /
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?
Image 1. No problem with sorting here, tree is one sorting layer behind my char
Image 2. With the same sorting layers my char will look like he's climbing the tree but he should be behind (swap sortinglayers at runtime or something)
Answer by JustFun · Aug 09, 2014 at 08:35 PM
You don't need to change sorting layer. You need to swap your tree and character sprites sorting order. http://docs.unity3d.com/ScriptReference/Renderer-sortingOrder.html . Sprite with bigger sorting order will be rendered in front of sprite with smaller sorting order.
Thank you for this, this solved it for me. Setting sortingOrder statically for all the static objects with their position.y and updating it with position.y for the char as I keep rendering. Cheers!
Answer by pooyafayyaz · Aug 09, 2014 at 07:59 PM
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.
Answer by ulysses 31 · Aug 10, 2014 at 09:52 AM
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.
Your answer
Follow this Question
Related Questions
Allow for sorting between different sorting layers 0 Answers
Soritng 2d objects 0 Answers
How do I set the rendering order of objects with the same sorting layer and order? 1 Answer
How to return tile based on heightmap efficiently? 0 Answers
CustomAxis sort not working between objects placed at different layers rendered by multiple cameras 0 Answers