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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sorting point not working in nested sorting group

Discussion in '2D' started by pittawat2200, Nov 13, 2022.

  1. pittawat2200

    pittawat2200

    Joined:
    Dec 3, 2017
    Posts:
    3
    Hello everyone.

    I want to create 2D top-down game. So, I'm start researching unity 2d feature and currently learning about sprite rendering part. I've found some useful stuff to decide which sprite to render first on the same sorting layer.

    By setting project Transparency Sort Mode to Custom Axis and Transparency Sort Axis to Y (0, 1, 0).

    upload_2022-11-13_23-14-11.png

    And I've been trying to do this
    • Create 2 parent empty GameObjects (With Sorting Group & No Sorting Group)
    • With Sorting Group is adding Sorting Group component and No Sorting Group do nothing.
    • Create 2 empty GameObjects (Red & Green)
    • Create 2 square sprites (Square Red & Square Green) assign to child of Red and Green by name.
    • Adding Sorting Group component as an offset pivot point to Red and Green then move sprite position up to give Sorting Group act like bottom center pivot point.
    • Duplicate Red and Green assign as child to parent GameObjects.
    like hierarchy image below.
    (The left is With Sorting Group and the right is No Sorting Group)
    upload_2022-11-13_23-55-26.png

    As you can see the problem is when it has nested Sorting Group (With Sorting Group). It look like a Y sorting point are not work properly. I don't know why? Can anybody help me explain about it?
    (I tried searching but couldn't find any related answer)


    PS. All properties mostly default when adding. I'm just changing the position and adding the Sorting Group component. (I'm using Unity 2021.3.12f1)
     

    Attached Files:

  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    What does your SpriteRenderer look like. There is a setting on there for using pivot or center to sort, by default it is center.
     
  3. pittawat2200

    pittawat2200

    Joined:
    Dec 3, 2017
    Posts:
    3
    Yes, All my sprites are default setting (sorting point = center). And I have tried change it to pivot but result still the same. I don't think sprite sort point will effect the sorting order. Because it got override by parent sorting group (that I've used as offset pivot point). I'm just curious is it bug or something?

    Results here.
    (Center sorting point)
    upload_2022-11-14_19-26-36.png

    (Pivot sorting point)
    upload_2022-11-14_19-37-2.png
     
  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    From what I understand that's by design. See the docs for sortinging renderers within a sorting group https://docs.unity3d.com/Manual/class-SortingGroup.html#InternalSort

    I assume that "Distance to camera" refers to sorting axis, since the general 2d sorting description places "Perspective/Orthographic/Custom axis and Sprite Sort point" as subheaders under "Distance to camera".


    I was initially also slightly surprised about this as I assumed sprites within sorting group are sorted exactly the same as outside. In practice it isn't that bad because amount and structures under sorting group are usually small enough that you can control ordering by assigning layers and position within layer, unless you are abusing nested sorting groups for what would better be handled as layers.

    Do you actually have a practical usecase where you want to use sorting axis within sorting group or were you just curios about why it didn't work?
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Oh yeah, once a renderer is part of the sorting group you need to use the order in layer property of the renderer to adjust the draw order. The order in layer property becomes local to the sorting group for each renderer. I believe this is an optimization to avoid having to do the "Distance to camera" calculation for each sprite in the group.

    In the end it works better, as you can ensure that something like the head of a character is in front of the body that would normally be behind it with y axis sorting.
     
  6. pittawat2200

    pittawat2200

    Joined:
    Dec 3, 2017
    Posts:
    3
    Oh, I get it. Thank you guys :D for answered my question. In my case I'm just curious about what I've missing or misunderstand something in docs. It cleared now.