- Home /
how to change the render sequence in 2D game ?
for example, the player stands in front of a tree , the player will cover the tree , when the player is behind the tree,the tree will cover the player,how to achieve that ? need i change the render sequence in the code ?
Answer by Nikaas · Dec 19, 2017 at 08:50 AM
Better than using Z transform is to use the Sorting Layer option on the SpriteRenderer component. There is additional option called Order In Layer - this number controls the draw order of sprites belonging to the same Sorting Layer. Order In Layer is effectively a subdivision of Sorting Layer.
Both can be accessed through code if you want to change them at runtime.
thank you for your answer. i want know why the Sorting Layer is better than using Z transform ? is it more effective ? and how do i know when to change the sprite's order in layer at runtime ( is using the z-coordinates ?but it's similar to use the Z transform)
You can think of it like: Z transform is subdivision of Order In Layer, OIL is subdivision of Sorting Layer. This way you can reorder draw order of groups of objects while retaining the draw order between all the objects in that group.
Answer by Larry-Dietz · Dec 19, 2017 at 07:13 AM
If you are just dealing with 2D sprites, changing the Z value in the transform position should change the render order.
player z = 1 tree z = 0 = player in front of tree player z = 0 tree z = 1 = player behind tree
You can change this z value based on the y values. If the Y value of the player is higher that the y value of the tree, change the players z value to a lower value than the trees z value.
Hope this helps, -Larry
Your answer
Follow this Question
Related Questions
Render Gradient Background At Last in 2D Game 0 Answers
Small line appearing under my 2D pixel sprite - 2D Graphics not rendering properly 2 Answers
2D bone based prefab render order 2 Answers
2D-Sorting; Does batching affect the order of 2D-Renderers? 1 Answer
Allow for sorting between different sorting layers 0 Answers