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
0
Question by TntManiak · Nov 19, 2015 at 02:51 PM · scripting problem2d gameunity 2dlayersspriterenderer

Handle order in layer in 2D game ?

Hi everybody,

I'm currently developing a new game for learning Unity2D. I'm trying to implement a 2.5D (is it really a 2.5D?) game type and I'm facing an issue concerning "order in layer".

Let's take an example. I have two game component :

  • Player which have 2 in "order in layer"

  • Crate which have 3 in "order in layer"

So if Player moves behind the crate, it's OK as you can see (admire this unique visual style) :

alt text

But if Player moves in front of the crate, it's weir because the player seems to be "behind" it, as you can see : alt text

If I manually change the player order to 4, it's OK. So here is my question :

How can I dynamically handle the player layer order depending on the Y axis, bearing in mind that the Y axis range which the player can move may vary because my level can go up and down ?

Thank you in advance for your help.

1.png (69.3 kB)
2.png (60.1 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TntManiak · Nov 20, 2015 at 08:07 AM

After all I managed to dynamically modify sorting order using Y value of objects. The script must receive of course a lot of improvements but here is the idea.

 using UnityEngine;
 using System.Collections.Generic;
 using System.Linq;
 
 public class SortingLayerManagement : MonoBehaviour {
 
     private List<Transform> transforms;
 
     // Use this for initialization
     void Start () {
         transforms = new List<Transform>();
         
         for (int i = 0; i < transform.childCount; i++)
         {
             Transform child = transform.GetChild(i);
 
             if (child.GetComponent<SpriteRenderer>() != null)
             {
                 transforms.Add(child);
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         // Tri les enfants par position
         transforms = transforms.OrderByDescending(
             t => t.FindChild("PerspectivePoint").position.y
             ).ToList();
 
         int layerOrder = 0;
         foreach (Transform child in transforms) {
             child.GetComponent<SpriteRenderer>().sortingOrder = layerOrder;
 
             layerOrder++;
         }
     }
 }
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
avatar image
1

Answer by Dinosaurs · Nov 19, 2015 at 04:29 PM

You can either treat your game as 3D rather than 2D and position sprites Z position yourself rather than using layers, or run a script that checks the Y value of game objects against the player and moves them accordingly.

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 TntManiak · Nov 20, 2015 at 08:09 AM 0
Share

Thank you for your advices, I finally come with a solution which approximates your second proposal.

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

36 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

Related Questions

Identifying 3 GameObjects in contact with each other,Identifying and destroying 3 Game Objects with the same LayerName 0 Answers

I need help with AI,Force not working 0 Answers

How to build a two sided sprite shader with zwrite on. 0 Answers

Best way to implement character(2d) interaction with other objects or characters 0 Answers

top down 2d shooter direction of enemies 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