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
0
Question by Goliadkin · Mar 23, 2015 at 09:20 AM · c#collider2dmousepositionboundsscreentoworldpoint

collider2D.bounds.Contains not working properly

I've got a GameController object with a square 2D collider that covers the whole screen. Inside that GameController there are 7 objects (Zones), each one with their own polygon collider. Here's the setup:

View

Hierarchy

What I'm trying to do is to check if the clicked position is inside any of those Zone's colliders whenever I click inside the big square collider.

This is the OnMouseDown() code of the GameController's script:

 void OnMouseDown ()
 {
     Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
     if(this.collider2D.bounds.Contains(mousePos)) Debug.Log ("1st Check");
 
     //'Zones' is a list with all the Zones GameObjects
     foreach (GameObject zone in Zones)
     {
         if(zone.collider2D.bounds.Contains(mousePos))
         {
             Debug.Log ("2nd Check");
         }
     }
 }

Shouldn't I ALWAYS get the "1st Check"? Because it's redundant: if the OnMouseDown() got triggered because I clicked inside the collider, then the mouse position has to be inside the collider. But I never do, so I think I'm missing something right here about the Input.mousePosition or the ScreenToWorldPoint()

2.png (5.8 kB)
1.png (53.3 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

3 Replies

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

Answer by ProtoTerminator · Mar 23, 2015 at 06:09 PM

Haven't tested, but it might be because you're trying to see if a 2D collider contains a 3D point. Cast your mousePos ((Vector2) mousePos) when you call the contains and see if that works.

Also, collider.bounds doesn't work so well with non-rectangular colliders. So for your zones, you may want to use collider.OverlapPoint instead. (And also you may want to fix up those colliders to match the visual representation.)

Comment
Add comment · Show 3 · 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 Goliadkin · Mar 24, 2015 at 12:56 AM 0
Share

It makes sense, I'll give it a try! I'll update whenever I get to try the Vector2 mousePos. Btw, those colliders have to be overlapping like that (even though they look cheap) because I use bounds.Intersects() to check if a zone is neighboring with another.

[EDIT] Had to work it out a little bit, doing

 this.gameObject.collider2D.bounds.Contains(Vector2(mousePos));

Would result in some errors, but doing

 Vector3 3dPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Vector2 mPos;
 mPos.x = 3dPos.x;
 mPos.y = 3dPos.y;

And then

 this.gameObject.collider2D.bounds.Contains(Vector2(mPos));

Works perfect.

Weird thing is that $$anonymous$$onodevelop says that .bounds.Contains() has to receive a Vector3.

Thanks a lot!

avatar image ProtoTerminator · Mar 24, 2015 at 06:19 AM 0
Share
 this.gameObject.collider2D.bounds.Contains((Vector2) mousePos);
 
 That's how to cast it without needing to do all the other stuff you did there. (parentheses go around the type, not the variable)
avatar image Goliadkin · Mar 24, 2015 at 06:52 PM 0
Share

Excellent, I didn't know that, thank you!

avatar image
0

Answer by JigneshKoradiya · Mar 23, 2015 at 08:06 PM

if you just want to chek you are clicking on zona than you dont need to use collider.bounds

just write in script

//this will give you event when you click on zona 1 if its drag on zona1

void OnMouseDown()

{ debug.log("clicked on zona1"); //put this script on zona1 gameobject and same for all 7 }

Comment
Add comment · Show 2 · 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 Goliadkin · Mar 24, 2015 at 12:50 AM 0
Share

Please read (or reread) the whole thread before answering. I need to check, whenever the On$$anonymous$$ouseDown() function triggers, if the clicked point is inside any of the 7 child Zones' colliders.

avatar image JigneshKoradiya · Mar 24, 2015 at 07:16 PM 0
Share

that are seprate child or different collider than its possible to get event of that each collider when click on it i read your thread another time

avatar image
0

Answer by aaonurdemir · Oct 06, 2016 at 01:15 PM

This should be a unity bug. If I use Collider2D and check whether another object is inside it, I should not consider z-position, should I?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

ScreenToWorldPoint and Mouse Position 2 Answers

Convert Mouse Position to move a cube on 1-axis. 1 Answer

Trying To Find Mouse Position On Tap 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