- Home /
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:
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()
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.)
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!
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)
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 }
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.
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
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?
Your answer
Follow this Question
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