Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by $$anonymous$$ · Apr 02, 2014 at 01:26 AM · 2dspritesorting layers

Best way to handle Sprite sorting in compound objects?

Hi all,

I'm currently testing Unity's 2D features, and am running into a problem with sorting GameObjects made out of several Sprites. Let's say I have a (very badly drawn) cat Prefab made out of parts, wherein the parts are in the same sorting layer, but in an order that sorts it properly - Tail and legs are order 0, body is order 1, and head is order 2 in the following:

alt text

The problem starts when I want to have a clowder of cats on screen, some overlapping each other like so:

alt text

The head belonging to the background cat hovers over the foreground cat's body. I want each cat to be in its own discrete "layer" - which could tentatively be done if the order in layer property could be localized to each prefab instance... but I don't know if it can.

Is there an elegant way to work around this problem?

Thanks for any help.

Comment
Add comment · Show 6
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 bendahmon · Nov 26, 2014 at 11:10 PM 0
Share

I'm looking for the same solution. Did you find a good way to achieve this?

avatar image scratchyback · Apr 13, 2015 at 03:58 PM 0
Share

Same here, I am looking to a solution to this problem too

avatar image hexagonius · Apr 13, 2015 at 04:06 PM 1
Share

How about a static integer, which counts up every time it's accessed. Each cat could then initialise their sprites in the order needed with the current value from the int

avatar image scratchyback · Apr 13, 2015 at 04:35 PM 0
Share

I have thought about that as well, the problem is though that you can only set the sorting layer and the sort order of a sprite, and not of the gameobject containing all these different sprites. So this means you have to increase the sort order of -all- underlying children of the gameobject...hmm..... I guess that is possible. I had hoped there would be something more elegant though :) But if none pop up I am gonna try your solution (and hope there is a way to count the underlying number of sprites a game object has so the code wont break if I add another sprite to the game object at a later point). Thanks for pointing me in that direction again!

avatar image $$anonymous$$ · Apr 13, 2015 at 06:56 PM 0
Share

hexagonius' method was how I ended up doing this. I didn't think it was that great an approach, but my peers couldn't come up with better ones :(

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Basher207 · Apr 13, 2015 at 07:02 PM

A simple way to do it is make a script that has a static variable, that starts at 0, for example:

static float catZOffset;

 void Start() {
  transform.position += new Vector3(0,0,catZOffset);
  catZOffset += 0.00001f;
 }

but my due its a good idea to have the cats head and body not so far away from each other, like make them into one sprite I think? if you can't do that because you are changing them individually in your game than I think you can just make there distance to each other closer, and make the catZOffset increment twice as big to make sure that no 2 cats overlap P.S. There might be something with the sprite rendere component that let you do that, and i'm just leading you on to do an overly complicated solution, but idk :D As far as I know thats the way to do it.

Comment
Add comment · Show 1 · 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 scratchyback · Apr 18, 2015 at 07:54 AM 0
Share

I tried setting the Z-index but in 2D the z-index seems to be ignored. So ins$$anonymous$$d I went for hexagonius method as well and increased the sorting order of each individual sprite to make the compound object as a whole appear in front of another

avatar image
0

Answer by bendahmon · Apr 18, 2015 at 08:45 PM

My solution to this was to set all objects (in-game stuff that is) on the same sorting layer and have order in layer = 0. Then I used the Z-coordinate to sort them all. This works fine for me. But it is not the way I wanted to do it, as I was hoping that the sorting layer concept was the solution for me.

Maybe I'm wrong but it seems like Unity orders like this: sorting layer, order in layer, z coord. So any object in the same sorting layer and order would be sorted by Z.

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

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

24 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

Related Questions

Shader vs Sprites - Sorting layer (ZWrite Off not working) 1 Answer

Sorting Layers? How? 1 Answer

Render sprite only on top of a specific sorting layer 1 Answer

[2D] [C#] Sprite disappears after changing position 1 Answer

Blood splats 2d that sticks only on one 2d sorting layer 0 Answers


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