Sorting layers according to Y axis
Hello - I am new to Unity, and I am trying to make a 2D, top-down, isometrical game. I am wondering how I can sort the layer of objects based on their position of the Y axis using a script. In other words, I am trying to create the effect of a background/foreground according to Y position. For example, if object_1 is beneath object_2, the layer for object_1 should be greater than the layer for object_2.
If anyone can help me, I would greatly appreciate it. Thanks!
Answer by MakinStuffLookGood · Nov 14, 2015 at 09:10 PM
Update for 2020:
Use Transparency Sort Mode on a Camera. You can conveniently set a custom axis project wide to sort based on Y axis instead of Z, mix with Sorting Groups to keep a whole character made of multiple sprites together and sort as a single entity.
Answer by Retruate · Aug 25, 2019 at 09:07 AM
Not to necropost, but since this is the first search result on Google & Bing, if anyone in is here in 2019, there is a much cleaner solution.
Edit > Project Settings > Graphics > Camera Settings > Transparency Sort Mode > Custom Axis ... > Transparency Sort Axis > (0, 1, 0)
Is there a way to do this where it sorts by the pivot point ins$$anonymous$$d of the center of the sprite?
Thanks for sharing this! This is indeed a much cleaner way of achieving this
Answer by sinedsem · Jan 26 at 03:18 PM
If you are using Universal Render Pipeline you will not see Camera settings in Graphics menu
Instead, in Assets create new asset of type Renderer 2D Data (Create/Rendering/Universal Render Pipeline/2D Renderer), configure transparency in it's inspector and then use it as a default renderer in yout URP object.