Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamic Sorting order

Discussion in '2D' started by sunnychan1337, Feb 27, 2021.

  1. sunnychan1337

    sunnychan1337

    Joined:
    Dec 14, 2020
    Posts:
    8
    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.
     
    Last edited: Feb 27, 2021
  2. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
  3. sunnychan1337

    sunnychan1337

    Joined:
    Dec 14, 2020
    Posts:
    8
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    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.