I have a character made of individual sprite parts. The parts itself are animated… i offset each piece a bit on the Z axis so they show in the correct order… but it doesn’t work 100%…
on animation… sometimes the sprites overlap!
I tried assigning layers to the parts so they don’t have to rely on Z, but this doesn’t work because my game world is 3d… when i move the character to the back of another character, having a different layer means it will always get rendered in the front (even when the character is at the back).
how can i correctly show different parts with correct order, while also preserving the correct order when moving around in 3d space?
when the sprites have 0 rotation, offsetting appears to work… but because all my sprites are on an angle to match the camera (about 50 degrees rotated), offsetting doesn’t work… I’ve made as much as .2 in z offset and once animation kicks in the sprites flickers badly.
i am stuck with using layers right now to prevent flickering, but its bad when characters overlap because of front/back position situations.
Another option (which may be a really bad idea) is to expose the render queue in the shader for each item and set them to be different based on the drawing order you want.
Maybe even just use something like this attached to each item:
using UnityEngine;
using System.Collections;
public class SetRenderQueue : MonoBehaviour {
//
// set this in the inspector to be some sane value for each item
//
public int queue = 1000;
void Start () {
GetComponent<Renderer>().material.renderQueue = queue;
}
}
Note, this may be a very bad solution, but it may work still
From what I’ve managed to pick up, Sprites have the same depth over their entire shape, since they are meant to be used in 2D only.
You can use a Quad and put the texture you want on top of it to get the same effect as a sprite without the weird overlapping, although it does complicate changing the sprite.
And while we are digging up seven-year-old necro posts to common “how does this game engine even work?” questions, here is my stored tribal knowledge about layering:
Three (3) ways that Unity draws / stacks / sorts / layers / overlays stuff:
In short,
The default 3D Renderers draw stuff according to Z depth - distance from camera.
SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties
UI Canvas Renderers draw in linear transform sequence, like a stack of papers
If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:
identify what you are using
search online for the combination of things you are doing and how to to achieve what you want.
There may be more than one solution to try.
Additional reading in the official docs:
And SortingGroups can also be extremely helpful in certain circumstances: