I’ve been searching if anyone is having the same problem but I can’t seem to find anything related, which is weird. Maybe I’m just missing something very simple.
I’m making a 2D game using a Perspective camera set at 5 fov for a slight sense of perspective, everything is working fine except one thing: Sorting Layers don’t seem to be working, at all. I’ve got a prefab of a tree with a trunk and foliage, some of which are in front of the trunk and others behind using the Sorting order of the same layer (Vegetation), but the camera doesn’t seem to care, it randomly renders the foliage in front or behind the trunk. Here’s a pic so you understand what I’m talking about:
As you can see the prefab has a parent (_treeSprite) with a Sorting Group that is set according to its Z position so near objects don’t render behind others further away. Then there’s the actual tree sprites using the Vegetation Sorting Layer and as I said some are in front and others behind, but you can clearly see on the game window it’s simply ignoring that and rendering randomly. Any idea why this may be happening? Do I have to change something on settings perhaps or is it a bug? Is the perspective camera doing this? I’ve tried restarting Unity but it solves nothing. Also I’d like to point out this was not happening on 2019.4 and started happening as I imported everything on 2020.1.
I still couldn’t resolve this but maybe I can shine some light to those who may find themselves with the same problem. The main problem are Sorting Groups: when using those Unity will consider every sprite in that group to have the same Distance to Camera, independently of their local position, this causes sprites inside the group to clip on each other randomly.
This should be solved by using the Sorting Layer of each Sprite Renderer but for whatever reason it doesn’t work as intended, the camera ignores it. I’ve tried changing the Transparency Mode of the camera, tried every option and axis, but still won’t work. Logically setting it to Ortographic should work, but it doesn’t.
In this particular case I solved the issue with the trees with a simple script:
using UnityEngine.Rendering;
public class SpriteZOrder : MonoBehaviour
{
private SortingGroup sortingGroup;
private void Awake()
{
sortingGroup = GetComponent<SortingGroup>();
var sortGroupOrder = (this.transform.parent.transform.position.z + this.transform.localPosition.z) * 10 * -1;
sortingGroup.sortingOrder = Mathf.RoundToInt(sortGroupOrder);
}
}
Basically what this does is give each group of sprites an order in layer based on the position of the parent object + the local position of that group of sprites. In this case I picked a local position of 1 and -1 to give the tree some volume but as little as 0.1/-0.1 works as well. Adding this script to every object on scene will give an order in layer based on the Z position of each.
It’s a shame I had to do this, I sure hope this doesn’t bring future problems but it’s the only thing I can think of for now. Obviously I’m still interested in knowing the reason why Sorting Layers don’t work when using Sorting Groups in a 3D camera. If anyone knows please tell me!
Is it the case that if you assign a single Sorting Group to the parent of the whole tree (so the tree as a whole essentially has a single z value when sorting against your character), and then assign Order to resolve all conflicts within the tree (not using z at all) it doesn’t work with a perspective camera?