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! :slight_smile:

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’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.

Take a look at the Sorting Layers tutorial : http://unity3d.com/learn/tutorials/modules/beginner/2d/sorting-layers

public SpriteRenderer spriteLayers;
public int spriteLayersSorting;
void Start () {
spriteLayersSorting = new int[spriteLayers.Length];
for (int i = 0; i < spriteLayers.Length; i++) {
spriteLayersSorting = spriteLayers .sortingOrder;
* }*
* }*
* void FixedUpdate(){*
* for (int i = 0; i < spriteLayers.Length; i++) {*
spriteLayers .sortingOrder = spriteLayersSorting + ((int)-transform.position.z);
* }*
* }*

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.