Search Unity

Order within a Sorting Group

Discussion in '2D' started by Wordsonplay, Jan 27, 2019.

  1. Wordsonplay

    Wordsonplay

    Joined:
    Apr 29, 2013
    Posts:
    21
    I'm making a 2D game representing a street scene with busses and pedestrains. I'm having issues with getting the sorting order to do the right thing. I have set up a custom Transparency Sort Mode which uses the Y-axis. That does what I want with regard to sorting globally. Then I have a Sorting Group on the bus so that when people get on the bus they inherit the busses position in the global sorting order.

    The problem is getting the people on the bus to sort correctly. They move around dynamically, but it appears that sorting within a group is fixed by the hierarchy structure (So I've foudn from experimenting. The documentation could be clearer on this front), rather than using the global sorting mode. This means the last person to get on the bus is drawn in front of all the other passengers regardless of when they are positioned.

    Is there any way to get the Sorting Group to apply the Transparency Sort Mode *within* the group? If not, is there another solution to easily sort the sprites based on their Y position?
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    I use this whenever I need to update the sorting order on Y axis.
    Code (CSharp):
    1. GetComponent<SpriteRenderer>().sortingOrder = Mathf.RoundToInt(transform.position.y * 100f) * -1 + Sort_Layer;
    Sort Layer is a public int just fyi.... use it instead of the normal sort layer on renderer