- Home /
Sprite Rendering Order Issue
Hello everyone,
Info
I am making a game similar to Don't Starve graphic-wise (Bill boarded 2D sprites inside a 3D environment) (Picture)
However I am using an orthographic camera as opposed to perspective with a 45 degree rotation around x-axis
The Problem
I noticed that some small sprites appear behind bigger ones even if they are positioned in front of them. After some research, I found out that Unity renders sprites based on the distance between the camera and CENTER of the sprite, therefore taller sprites will have centers that are closer to the camera. That is why this does not happen with objects that are the same height.
My theoretical solution is to tell Unity to use the BOTTOM of the sprites instead. That would work best but I do not know how to do that (if it is even possible). Any ideas?
Thank you in advance! :)
EDIT 1
Another way of saying "My theoretical solution is to tell Unity to use the BOTTOM of the sprites instead" is that I want sprites to be ordered only based on their z position.
I'm currently in development of a billboarded 3d game as well and have ran into this issue several times. Was there any resolution or workaround that you've found?
Answer by darkerparker7 · Aug 29, 2018 at 02:48 AM
I've been banging my head against the wall with this problem for a long time and could not find a solution until today!
What I ended up doing to fix this exact problem was place everything under an empty GameObject container, and then simply rotate that while the camera stays in one place. I then added a custom transparency sort axis (x = 0, y = 0, z = 1). Works like a charm! I'm not sure what this means for moving about inside that container, but I don't think that is as big of an issue. Even with such a steep camera angle, still works fine.
Super excited about this! I am making a Realm of the Mad God demo which requires the same art style.
Thank you for your answer darkerparker7 :) I don't think the transparency sort axis was in the version of Unity I was using when I posted this question. It's great to see awesome features like that being added, and I really appreciate you telling me about it!
Answer by contab009 · Jul 31, 2014 at 09:30 PM
Take a look at the Sorting Layers tutorial : http://unity3d.com/learn/tutorials/modules/beginner/2d/sorting-layers
I know about Sorting Layers. I want sprites to be ordered automatically based on their z position. $$anonymous$$y game is not set in a flat 2D world. I know I can make a script to order sprites every frame based on their z position but I want to know if there is a more efficient way of doing this. I also know that I can set the sortingOrder to the z position of sprite but that is my last resort.
I posted an answer that I've been using for awhile that should work.
Answer by Shiro_Rin · May 19, 2017 at 01:44 AM
public SpriteRenderer[] spriteLayers;
public int[] spriteLayersSorting;
void Start () {
spriteLayersSorting = new int[spriteLayers.Length];
for (int i = 0; i < spriteLayers.Length; i++) {
spriteLayersSorting [i] = spriteLayers [i].sortingOrder;
}
}
void FixedUpdate(){
for (int i = 0; i < spriteLayers.Length; i++) {
spriteLayers [i].sortingOrder = spriteLayersSorting [i] + ((int)-transform.position.z);
}
}
Answer by luoboa · Apr 15, 2020 at 12:33 AM
I was having a similar issue with wanting to sort based on z value.
If you go Project Settings > Graphics > Transparency Sort Mode and set it to Custom Axis and define the custom axis as 0, 0, 1 (so based on z values) it will sort based on z values.