- Home /
Some BoxCollider2D don't get hitted by no apparent reason
I have an sprite with several BoxCollider2D attached, it is like a keyboard so each collider is associated to one of the keys. Now, the problem is that I cast a ray when the mouse clicks like:
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (e.mousePosition), Vector2.zero);
It works perfectly for the first row, but it always returns null when a collider of the second or third row is clicked. All colliders are equal except for their X e Y coordinates. In fact, I created the first and copied and pasted it to create the rest, just moving each to the right place.
Moreover, to check if it was a problem with the colliders themselves, I disabled the H collider in the first row and put in its place the one that is for the I key in the second row, and then when clicking on such I collider that does not work when it is in the second row, the collision was perfectly detected while it was moved to the first row. Put it in its place on the second row again just changing the X and Y coordinates, and it stop working.
Any ideas?
Thanks a lot in advance.
EDIT: I put a sprite that moves to the mouse coordinates when I click and just have realized there is something wrong with the mouse coords. It is like if the Y-coords were inverted by some reason, and so the first row works well because it is in the middle of the screen, but when I press in the second row, the click is shown above the first row instead of below it.
I have checked the camera is not rotated at all, so I do not know why this is happening. As a work around I can just invert the Y-coord before calling Physics2D.Raycast, but I would prefer to solve it the right way, since it may be a problem for other things later two.
Answer by robertbu · Oct 25, 2014 at 06:00 PM
You don't show us all the code, but I'll bet that 'e.mousePosition' is from a Event. Event coordinates are GUI coordinates, which are not the same as Screen coordinates. You can use Input.mousePosition instead for the mouse position, or you can convert the GUI mouse position to a Screen mouse position:
Vector3 v = e.mousePosition;
v.y = Screen.height - v.y;
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (v), Vector2.zero);
e is Event.current. There is no difference between Event.current.mousePosition.y or Input.mousePosition.y for me. In both cases Y-coords are upside down. The only thing that worked is to invert the Y as I told before, but I was not satisfied with that response because I didn't now why I have to do that. Now I know it, so I did it this way:
Vector2 screenCoords = new Vector2 (e.mousePosition.x, Screen.height - e.mousePosition.y);
You don't need a Vector3 for that matter, I think. I will mark your answer as accepted, though, since the idea and the result it's the same.
Your answer
Follow this Question
Related Questions
RaycastHit Collider Question 1 Answer
Raycast always hit 1 Answer
colliders vs raycast vs physics.overlap? 0 Answers
Why isn't my RaycastHit2D working? 1 Answer
Hit distance Change light range 1 Answer