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
1
Question by carlo68 · Jul 14, 2014 at 03:37 PM · 2dcolliderinputorder

Order of 2D Colliders

I can't quite understand the behavior of Unity 2D when it comes to handling user input. Hope that someone can help me.

I have a number of sprites, and they may overlap. I can handle the drawing order of the sprites quite easily by using the "Sorting Layer" and "Order in Layer".

In order to handle user input, I define 2d colliders for relevant objects. Those objects receive user input (OnMouseDown(), etc.)

Problems arise when sprites overlap. While the Sorting Layer is used for rendering, it seems that it is not used for delivering user input to the right (that is, topmost in the rendering order) sprite having a 2D collider. So input seems to go randomly to one of the overlapping sprites, and the behavior does not even seem to be deterministic. How can I direct the input to the right (topmost) sprite game object?

Comment
Add comment · Show 3
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 carlo68 · Jul 16, 2014 at 08:01 AM 0
Share

Anyone? There has to be a way to deter$$anonymous$$e the frontmost object receiving the input, having a random object receive the input is simply unusable. So far I could not come up with an efficient way to do it, though.

avatar image Pyrian · Jul 16, 2014 at 02:07 PM 0
Share

Have you tried using Z-position values?

avatar image carlo68 · Jul 16, 2014 at 03:42 PM 0
Share

Yes, z position values made the difference. Since I am working on a 2D-game, all the z-position values were the same (0). Thanks a lot!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Jul 16, 2014 at 03:52 PM

Sorting Layer is related with rendering. Although you are developing a 2D game, actually you are working on a 3D system. Your sprites are on same index "Z" in Z layer. User touches and moues touches does not have a "Z" index. I mean it's 0. You can use "Z" value of sprites to determine which stands front and which stands at back. Position of camera is important too.I recommend you to switch to 3D view and see the order of sprites as you play with "Z" values. Here is a code script that i use to give "z" index to my touch:

Following code moves the touch point to -10 in "Z" and tiggers a check from -10 to +10 (due to given length: 20)

     private bool IsDialogHit (Vector3 touchPosition)
                 {
                         touchPosition.z = -10;
                     
                         //First let's check if there is an ui element. In that case return null. It's handled by ui.
                     
                         RaycastHit2D uiHit = Physics2D.Raycast (touchPosition, Vector2.zero, 20, _onlyUiMask);
                         return uiHit.collider != null;
                 }

 

Also you must be sure that Screen Positions are translated to World Positions by (_main the the Main Camera we use; it changes the position in screen to our world point system):

 Vector3 touchPosition = _main.ScreenToWorldPoint (touch.position);

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Combining multiple box colliders into one edge collider 0 Answers

Does Unity support 2D compound colliders? 1 Answer

Melee Attacking script doesn't work 1 Answer

OnCollisionEnter2D not working? 2 Answers

How can I edit a polygon collider2D? 7 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