Dynamic Sorting order

Hi all,

I am making a 2d game where I want dynamic sorting orders. Just as an example, when I have the player stand in front of a tree, I want the player rendered on top, but when the player moves behind a tree, I want the tree to render on top. Now if I were simply working with regular sprites, this would be easy. I could just tweak the Z values, based on the Y values. But the problem is that I am using rigged characters with each sprite, on a different sorting order. This means all the limbs have a different sorting order. So if I start tweaking Z values now, the only limb that gets affected, is the one that has the same sorting order as my tilemap. Is it possible to achieve what I want with skinned characters? If the “order in layer” parameter was a float, I could actually make it work because then I would be able to set the sorting value of the left arm to a value of “Y pos + 0.001” and the value of the right arm “Y pos + 0.002” and so on. But alas, this isn’t an option.

The only viable method I’ve thought of so far is to create a “collision box” of sorts behind all the houses/trees/rocks etc that detect when a player steps into that zone, and then changes their sorting order respectively. But this seems rather resource intensive. This way I have regular collision boxes and additional triggers zones. I can imagine that with a lot of objects and characters in the scene, it’s going to be insanely resource intensive, iterating through 2 sets of collision boxes.

Edit: How resource intensive is collision detection if you’re primarily using box colliders? Is it reasonable to have thousands? If thousands of colliders is fine, I suppose my proposed method isn’t too unreasonable.

Sorting Groups - Unity - Manual

A short Unity video: Designing With 2D Game Tools - Sorting Groups and Transparency Sort Axis

Thanks for the link. I’ve tried that setting before and for some reason it doesn’t do anything for me. Not sure if it’s because I use the universal render pipeline in my project. Ill play around with that setting in a fresh project to try and figure out if there’s something in my project that’s interfering with that setting.

If you’re using the experimental 2D Renderer with URP then you can set a custom transparency sort axis on the renderer asset itself.

Sorting Groups sound like a good solution for the problem you’re describing, particularly the tree problem.