Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

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:
    61
    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:
    [​IMG]

    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. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    1,946