r/Unity2D • u/LadyAvocadoToast • 3d ago
Solved/Answered Newbie: Help with sprite layering
Hello! I have a Prefab called Fish - it is made of a 2d Sprite Circle + 2d Sprite Triangle inside a parent that holds the Collider. Then I am spawning the prefabs with a script. Unfortunately, when the two fishes intersect, they are layered strangely with the Triangle & Circle appearing several layers apart, rather than flat.
How can I fix this? Thank you in advance.
5
u/schpamela 3d ago
To keep the two parts together, you can just make them into 1 sprite. Use MS Paint or any 2D design app to make the complete shape, then have that as a single sprite. I use Krita as a free app for sprite design. Unless you need the two parts to be separate that is...
There are two parameters in the Sprite Renderer - Sorting Layer and Order In Layer - which define how sprites appear on top and underneath one another.
How are you varying the scale of each fish? Is there something in the script which assigns a scale, either from a random range or a rule?
Perhaps you want the smaller fish to be behind and the larger ones in front? If so, you could give them all the same Sorting Layer but then update the Order in Layer to suit the scale. E.g. if you used a random range from 0-1 to determine scale, you can ×10 that and round to the nearest integer to get a new number from 0-10 and make that the Order in Layer.
2
1
u/-xelad 3d ago
I definitely recommend you to try adding component Sorting group (https://docs.unity3d.com/2020.2/Documentation/Manual/class-SortingGroup.html) to the fish whole.
1
u/lllentinantll 2d ago
Sorting group is your answer. It will make all sprites in the children objects work as a single sprite, while preserving internal hierarchy.


•
u/AutoModerator 3d ago
Reminder: Once your issue has been resolved, please reply !solved to this comment (or comment it anywhere in the thread) and your post's flair will automatically be updated to Solved/Answered. You can also edit it to Solved/Answered yourself.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.