Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by Jetyl · May 23, 2016 at 03:49 AM · spritessorting layerssorting order

dynamically sorting sprites in a 2d oblique perpective

so I am working on a 2d point and click game where the player moves around in an what is an illusion of 3d space, and for various reasons, I want to keep this project in 2D (the work flow is just better for what I'm doing), however, I'm hitting an issue with getting the player sprite to change its order in the sorting layer as I want it to.

see, I have a project that is emulating this style here, where there player's sprite would need to appear in front of the desk, when in front of it (duh), and in this case, on the left side of it, while it would need to be rendered behind it while on the right, and behind it.

I have scoured the internet looking to see if anyone else has had this issue, or come up with a solution, but I've come up nil.

I have tried working on this myself, and I did get a version of it to work, when the player only needs to work around a single object, but attempting to apply it on a scene with multiple objects the player can walk around breaks it.

here's what I have so far (warning as it is a giant mess):

 void Sort()
     {
 
         var backOrder = manga.sortingOrder;
         var frontOrder = manga.sortingOrder;
         var order = 0;
 
         foreach(var item in DynamicLayering)
         {
             var visual = item.GetComponent<SpriteRenderer>();
 
             var size = new Vector2(visual.sprite.bounds.extents.x * item.transform.localScale.x, visual.sprite.bounds.extents.y * item.transform.localScale.y);
 
             var pos = new Vector2(item.transform.position.x - size.x, item.transform.position.y - size.y);
 
             var rect = new Rect(pos, size * 2);
             
 
             //if the object's front is infront of the player 
             if(item.transform.position.y - size.y < transform.parent.position.y)
             {
 
 
                 if(item.transform.position.y < transform.parent.position.y)
                 {
                     //so the order taken is the farthest back
                     if (backOrder >= visual.sortingOrder)
                     {
                         backOrder = visual.sortingOrder - 1;
                     }
                     continue;
                 }
 
 
                 var playrect = new Rect(new Vector2(transform.position.x - playsize.x, transform.position.y - playsize.y), playsize * 2);
                 
                 //if the object is in the same plane as the player
                 if (rect.Overlaps(playrect) && rect.width > 0.5)
                 {
                     
                     //if the player is on the visible side
                     if (item.transform.position.x < transform.parent.position.x && leftsideHidden)
                     {
 
                         //so the order taken is the farthest front
                         if (frontOrder <= visual.sortingOrder)
                         {
                             frontOrder = visual.sortingOrder + 1;
                             
                         }
                         continue;
                     }
 
                     if (item.transform.position.x > transform.parent.position.x && !leftsideHidden)
                     {
 
                         //so the order taken is the farthest front
                         if (frontOrder <= visual.sortingOrder)
                         {
                             frontOrder = visual.sortingOrder + 1;
                             
                         }
                         continue;
                     }
                 }
 
                 //so the order taken is the farthest back
                 if (backOrder >= visual.sortingOrder)
                 {
                     backOrder = visual.sortingOrder - 1;
                 }
 
                 
             }
             else
             {
                 //so the order taken is the farthest front
                 if (frontOrder <= visual.sortingOrder)
                 {
                     frontOrder = visual.sortingOrder + 1;
                 }
             }
 
         }
 
         //determining the order position we are in
         if (frontOrder == manga.sortingOrder && backOrder != manga.sortingOrder)
         {
             order = backOrder;
             //print("back");
         }
             
         else if (frontOrder != manga.sortingOrder && backOrder == manga.sortingOrder)
         {
             order = frontOrder;
             //print("Front");
         }
             
        else if (frontOrder == backOrder)
         {
             order = frontOrder;
             //print("same");
         }
             
         else
         {
 
             //what to do if both sides off?
             order = frontOrder;
             //print("old");
         }
             
 
         manga.sortingOrder = order;
         //print(manga.sortingOrder);
     }

I am not at all proud of this code, but this is what I got. its called every frame when the player is moving, loops through the array of objects in its sorting order that are relevant to it (it is filtered to that point elsewhere), to find where its order is to be sorted (objects in the scene have a higher value in the sorting the "closer" they are to the foreground). I feel like my logic is in the right direction, but I'm completely lost on its implementation.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

56 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

Related Questions

line renderer sorting order? 1 Answer

Transparency Sort Mode 0 Answers

Sprite sorting order, background behind middleground but in front of foreground 1 Answer

GameObject can be seen through panel inside a canvas. 1 Answer

Can't get the sorting layers to work programmatically 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