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 /
avatar image
0
Question by No_Username_Found · Dec 20, 2017 at 02:46 AM · spritesorting order

Sprite Sorting Layers in 3d Environment

The goal I have in mind is a world like that in Don't Starve; a 3d area but all characters and objects are 2D sprites. I am having a problem with sorting layers, sorting order, and objects that are 3D behind a sprite rending as if they were 3D in front of that sprite.

Right now I am just working with trees. A tree that grows at 0,0,0 should be completely in front of a tree at 0,0,10 when they overlap in the camera view. This can be controlled by making the sprite's "sorting order" more negative as the z value increases (the further in the background, the more negative the sorting order).

Here is my problem: when I define the sorting order for an object, Unity automatically and instantly changes the number to something else. Here is my code:

 //create the object
         GameObject newGrowth = Instantiate (rBase, gameObject.transform.position, gameObject.transform.rotation);
     //set the sprite order
     baseOrder = (int)(transform.position.z * -10000); //the multiplier is arbitrary, but higher numbers produce the bug more often than lower numbers
     newGrowth.GetComponent<SpriteRenderer> ().sortingOrder = baseOrder;
     Debug.Log ("Base order: " + baseOrder);
     Debug.Log ("Sorting order: " + newGrowth.GetComponent<SpriteRenderer> ().sortingOrder);

Because I'm setting the sortingOrder = baseOrder, the outputs should be the same. However, here is the output:

 Base order: -448519
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Sorting order: 10233
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)

When the multiplier ( * 10000) is smaller the error happens less often but it does still occur, so it doesn't seem to be only related to some hard coded upper limit (if one exists). The weirdest part is that this doesn't happen with every object. Here is the output of the same code when I spawn in multiple objects.

 Base order: -31282
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Sorting order: -31282
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Base order: -59923
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Sorting order: 5613
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Base order: -28216
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Sorting order: -28216
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Base order: -43937
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:108)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)
 
 Sorting order: 21599
 UnityEngine.Debug:Log(Object)
 Plant_Procedural:SetBase() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:109)
 Plant_Procedural:Start() (at Assets/Resources/_Scripts/Plants/Plant_Procedural.cs:61)

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

97 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

Related Questions

Renderer sortingOrder performance question 0 Answers

Sorting Layer Logic Issue 0 Answers

Non-Linear Sorting Order of 2D Sprites 0 Answers

Draw call depending of sorting Order of SpriteRedenderer(2D) 2 Answers

How do I rearrange sprites' transform coordinates by name? 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