Search Unity

Complex characters vs render order

Discussion in '2D' started by sshepelevich, Jan 12, 2014.

  1. sshepelevich

    sshepelevich

    Joined:
    Nov 21, 2013
    Posts:
    10
    Hi guyz,

    I have created complex characters consisting of several sprites and animated them using Unity's native animation tools.
    I intend to use many moving copies of the characters at the screen at once. However, when characters overlap each other, I get result like at the attached image. It happens because same parts of different characters have the same z order. What we need here is to specify somehow that all children's Z positions are relative to the root game object and not to global world.

    Any thoughts will be highly appreciated.

    $sorting-order.jpg
     
  2. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Can you put your separate characters on different sorting layers? Then the individual parts are ordered, but within the character's sorting layer. Depending if your characters are dynamically created or there are lots of them this may or may not be practical.
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You can control the sorting order programatically. In order: sortingLayerName, sortingOrder, and then the object z. So there are a couple of ways you can approach this.

    Complex, but robust:
    Have something that controls the sort order. Typically whatever you have spawning them. Since it could know about the others, it can keep track and pass the appropriate layer information to the object. You would also need something on the object that will run through the object and set things. (basically kind of emulating the features of display object container in Flash.

    Easy but less controlled:
    Since order is drawn by z-depth later in the rendering, you can put all your parts on the same layer and order. Since they are all inside a container then just manually adjust the z depth of all the parts until they are in the right order. (use as little a shift as possible). You can now adjust the z depth of the parent, and as long as its depth is greater (or less) than the total combined depth of the parts, the whole "set" will shift.

    Here is a quick example, with the 2d view, and a view from above. All the parts have the same layer name and order.
    $Screen Shot 2014-01-12 at 9.16.18 PM.png $Screen Shot 2014-01-12 at 9.16.49 PM.png

    This way is quick, but can get a little complicated if you are dealing with physics/collision.
     
  4. RewindGameStudio

    RewindGameStudio

    Joined:
    Oct 21, 2013
    Posts:
    194
    We're experiencing the same issue. Would like to know the recommended approach as the same thing happens in the Unity 2D platformer demo if you duplicate the main character. Not sure what the limit on sorting layers is. Sounds like it would be bad for each object to have it's own :(
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    It's not really an "issue". You just have plan things out. Determine what you ultimately need, and build an approach based on that. No, normally you don't want to have each character on a layer. (though you may, depending on the game type)

    - Layers are typically for grouping (background/foreground/GUI/Etc)
    - Sort order controls the order of the part or element on the layer. (You can have 65k levels)
    - Z depth can also be used if you need more that 65k layers.