• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kingjohnc19 · Dec 04, 2018 at 01:04 PM · 2d sprites

How do I make the player sprite on top of the object when the player is above it but below the object when the player is below it?

So i'm making a top-down RPG game and I want the player to appear behind the tree (like shown in the first image). But the problem is, I want to make the player above the tree when they are in front of it. If you look at the second image, the tree will be over the player when I want it to be under them. How can I fix this?

screenshot-11.png (182.0 kB)
screenshot-12.png (182.9 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PizzaPie · Dec 04, 2018 at 01:35 PM 0
Share

Assu$$anonymous$$g you are using sprite renderer you can change the sorting layers. Leave the trees on default and add a new one (Player) and assign player object to it.

avatar image kingjohnc19 PizzaPie · Dec 07, 2018 at 12:26 AM 0
Share

But if I do that, the player will appear to walk on top of the tree ins$$anonymous$$d of behind it like shown in the first image.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Dec 07, 2018 at 02:05 AM

New answer with an interesting idea - may not be the best. The idea stems around the idea that the y position of the character determines if it's behind a tree or not. If the y of the character is greater than that of the tree, then the character should be behind it - and vice versa. So...


 using UnityEngine;


 /// <summary>
 /// A singleon manager that has a reference to the player
 /// </summary>
 public class TreeSortManager : MonoBehaviour
 {
     // Singleton
     static TreeSortManager instance; // we keep a reference to our own script... its static

     // Use static property to use the reference to this script, giving access to variables, methods, etc.
     // It similar to how you would use a static class
     public static TreeSortManager Instance
     {
         get
         {   // reference will always be null, until we find one
             if (instance == null)
         {
             // search our scene with this script and reference it
             instance = (TreeSortManager)FindObjectOfType(typeof(TreeSortManager));
         }

         return instance;
     }
 }

    [SerializeField] GameObject player;
    public GameObject Player { get { return player; } }
 }



I created a manager (you probably have one) that has a reference to the character. I just made a simple singleton and reference the character to it in the inspector.


 using UnityEngine;
 
 
 // Tree sort of awesome
 public class TreeSort : MonoBehaviour
 {
     [SerializeField] SpriteRenderer renderer;   // Reference your sprite renderer in the inspector
     GameObject player;
     
     int currentSortOrder = 1;   // default to all trees in front
 
     const int IN_FRONT = 1;
     const int IN_BACK = -1;
 
 
     void Awake()
     {
         player = TreeSortManager.Instance.Player;
         SetSortOrder();
     }
 
 
     void SetSortOrder()
     {
         renderer.sortingOrder = currentSortOrder;
     }
 
 
     void Update()
     {
         int order = (player.transform.position.y >= transform.position.y) ? IN_BACK : IN_FRONT;
         if (order != currentSortOrder)
         {
             currentSortOrder = order;
             SetSortOrder();
         }
     }
 }


Then I created a TreeSort class that is on every tree. All it does is determine if the tree should be behind the player (or front) via comparing their y position values.


The key here is that for my tests, the character and trees pivot points where in the center of the image. I think this is crucial in this idea working correctly, but even if they aren't, you can add some sort of offset.


Hope this idea helps and sparks some other awesome ideas.


Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kingjohnc19 · Dec 08, 2018 at 01:12 AM 0
Share

I'm kind of new to Unity, and I don't know what a singleton is or how to make one. I'm sorry that I am taking so long on this question, but can you please explain it? thank you.

avatar image Vega4Life kingjohnc19 · Dec 08, 2018 at 01:26 AM 0
Share

Let me edit my generic singleton above with one you can just copy. Give me a few seconds.


A singleton just makes it so there will only ever be one of that script in your game. Right, so there is no reason to have another TreeSort$$anonymous$$anager. It also allows us to have instance access to the script via its static variable assigned to itself. Thus, you can do this TreeSort$$anonymous$$anager.Instance.Player - we get direct access to the manager (without getting a reference), so we can get the Player variable. Usually all your managers will be a singleton - also, you can just make a static class... somewhat similar.

avatar image Vega4Life kingjohnc19 · Dec 08, 2018 at 01:35 AM 0
Share

I updated the treeSort$$anonymous$$anager to have the singleton in it. Just copy the script and place it in your scene. Reference your player character to it, then add the treesort to all your trees.


Basically, just follow the instructions I already have up there.

avatar image kingjohnc19 Vega4Life · Dec 08, 2018 at 02:17 AM 0
Share

Thank you so much! Im sorry but I have just one more question. What do I set for the renderer on the TreeSort script? thanks!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Sprite blocks onPointerClick? 0 Answers

Gaps in sprites 0 Answers

2D Undertale like Cutscenes / Animations,2D Undertale type cutscenes / animations. 0 Answers

Is tilemap renderer sorting done by camera y position? 0 Answers

2D Bone option not showing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges