- Home /
Sprite Sorting Layers in 3d Environment
The goal I have in mind is a world like that in Don't Starve; a 3d area but all characters and objects are 2D sprites. I am having a problem with sorting layers, sorting order, and objects that are 3D behind a sprite rending as if they were 3D in front of that sprite.
Right now I am just working with trees. A tree that grows at 0,0,0 should be completely in front of a tree at 0,0,10 when they overlap in the camera view. This can be controlled by making the sprite's "sorting order" more negative as the z value increases (the further in the background, the more negative the sorting order).
Here is my problem: when I define the sorting order for an object, Unity automatically and instantly changes the number to something else. Here is my code:
//create the object
GameObject newGrowth = Instantiate (rBase, gameObject.transform.position, gameObject.transform.rotation);
//set the sprite order
baseOrder = (int)(transform.position.z * -10000); //the multiplier is arbitrary, but higher numbers produce the bug more often than lower numbers
newGrowth.GetComponent<SpriteRenderer> ().sortingOrder = baseOrder;
Debug.Log ("Base order: " + baseOrder);
Debug.Log ("Sorting order: " + newGrowth.GetComponent<SpriteRenderer> ().sortingOrder);
Because I'm setting the sortingOrder = baseOrder, the outputs should be the same. However, here is the output:
Base order: -448519
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Sorting order: 10233
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
When the multiplier ( * 10000) is smaller the error happens less often but it does still occur, so it doesn't seem to be only related to some hard coded upper limit (if one exists). The weirdest part is that this doesn't happen with every object. Here is the output of the same code when I spawn in multiple objects.
Base order: -31282
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Sorting order: -31282
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Base order: -59923
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Sorting order: 5613
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Base order: -28216
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Sorting order: -28216
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Base order: -43937
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
Sorting order: 21599
UnityEngine.Debug:Log(Object)
Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)