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

Sprite Sorting relative to Parent

Discussion in '2D' started by robertsze, May 21, 2015.

Thread Status:
Not open for further replies.
  1. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    We just stumbled upon a problem we seem not be able to solve. Hope you guys have an idea.

    Lets say we have a Car GameObject which has a SpriteRenderer for the Car image (Side View) and two child GameObjects with a SpriteRenderer for the front and back wheel (Side View).

    Sort Layer is set to zero for all GameObjects. Order in layer is 0 for the Car and 1 for the front and back wheels. Works perfect. One Car with two wheels on top of it.

    But now comes the problem. Lets say we spawn hundreds of cars driving on the same Y axis from left to right. Because the wheels have Order In Layer set to 1 (the wheels should be on top of the Car Image), the wheels will draw over all other Car Body images. So, any idea how to solve this?

    Basically we are looking for a draw order like this:
    CarBody1
    CarWheel1
    CarBody2
    CarWheel2
    CarBody3
    CarWheel3
    ....

    At the moment it is drawn like this:
    Car1
    Car2
    Car3
    Wheel1
    Wheel2
    Wheel3

    Because of the dynamic amount of Cars we spawn and other layers setting a unique Sort Layer for each Car is not an option. (And to be honest, I don't think making layers like Car1, Car2, Car3, Car4 is what Sort layers was designed for).

    We look for a way to tell Unity to sort a child object relative to its parent object sort. Any ideas how to solve this?

    Thanks!
     

    Attached Files:

  2. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Just look at it a different way. Use different sorting groups based on the vehicle components. Either make a layer for Wheels and a layer for Body or just have a Car layer and dedicate Order IDs 0 to 99 for Wheels and 100 to 199 for Car Bodies.
     
  3. robertsze

    robertsze

    Joined:
    Jul 8, 2013
    Posts:
    83
    Of course this will work, but I highly doubt this is what sorting layers have been designed for. Having to manually give unique IDs to each and every vehicle in each and every level....

    Whats missing is just something which allows to define a Sprite Renderer to use a relative index to the parent, so that Unity orders the draw vertices array like:

    Sorting Layer 0
    ...Order 0
    ...Order 1
    Layer 1
    ...Order 0
    Layer 2
    ...Order 0
    ......Child 1
    ......Child 2
    ......Child 3
    .........Child 1
    Layer 3
    etc...

    Actually this is like every other 2D framework handles draw ordering. If there is a Unity Dev around, is this something I can hack into myself by "extending" Sprite Renderer? Or maybe you have something like that on your roadmap?

    You may also read this post I found : http://www.aymericlamboley.fr/blog/unity2d-the-good-the-bad-and-the-ugly/ @ Order In Layer. Exactly same issue.
     
  4. PurplishCat

    PurplishCat

    Joined:
    Apr 24, 2019
    Posts:
    1
    A little late, but for others finding this thread trying to do the same thing, it looks like adding a Sorting Group to the parent object does exactly what you want.
     
  5. ZaherHa

    ZaherHa

    Joined:
    Jul 28, 2018
    Posts:
    5
    Perfecto
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,525
    Please don't necro threads like this. Please use the "Like" button, it's what it's for.

    Thanks,
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.