1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

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

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

  1. Autoface


    Sep 23, 2013
    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):
    2. void Start ()
    3.     {
    4.         gameObject.GetComponent<SpriteRenderer> ().sortingOrder = (int)gameObject.transform.position.y * -1;
    5.     }
    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. jeffreyschoch


    Jan 21, 2015