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.