Search Unity

Using sprite renderer "order in layer" for depth... Help needed :(

Discussion in '2D' started by Autoface, May 18, 2017.

  1. Autoface

    Autoface

    Joined:
    Sep 23, 2013
    Posts:
    112
    Hi, I'm creating a game which generates a large map for the player to explore. The map is made up of lots and lots of sprites. They are terrain sprites and prop sprites like trees. Each of the sprites has a variation of the following code attached which sets the layer of the sprite renderer based on the y position:
    Code (CSharp):
    1.  
    2. void Start ()
    3.     {
    4.         gameObject.GetComponent<SpriteRenderer> ().sortingOrder = (int)gameObject.transform.position.y * -1;
    5.     }
    6.  
    The script works find in regards to setting the layer position.

    The problem I'm having is with the Character.
    What I'm trying to do is make it possible for my character to run in front of and behind trees and other objects. Currently the character is made up of multiple sprites and each of them have their own layer in a separate character order layer as shown in the image bellow:


    I need to implement something similar in Update() for the player which adjusts the layer based on the y position but it needs to take in to consideration all of the sprites and layers in the character object.

    I have considered changing the z position instead but I would really rather not.

    Does any one have any ideas how I could go about getting this done?


    Many thanks!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    shoopi, Zyblade, vedram and 1 other person like this.
  3. grentle

    grentle

    Joined:
    May 14, 2016
    Posts:
    7
    THANK YOU! This helped me today. I was setting the sorting order manually, but it still wasnt working.

    TY, TY, TY
     
    LiterallyJeff likes this.