- Home /
Detecting if mouse position is inside rect
This is supposed to be easy but for some reason its not working the way I think it should
I have a rect set up
public static Rect BottomRegion = new Rect(Screen.height/2, Screen.width/2 + Screen.width/8,580,64);
and I want to detect if the mouse position is inside the rect so I created the following:
if(BottomRegion.Contains(Input.mousePosition))
{
Debug.Log ("Inside");
}
Pretty straight forward right?
Well when I detect my mouse position everything is backwards. The rect is located on the bottom of the screen, but the debug statement shows when the mouse is on the top of the screen.
the rect also defines where a gui box is located, and I can see the guiBox at the bottom of the screen to verify that the rect is set up right.
Answer by MathieuBarbier · Aug 21, 2012 at 08:44 PM
y are inversed in those referentials. Use mousePosition.x and (Screen.Height-mousePosition.y).
Vector2 mousePos = new Vector2(Input.mousePosition.x,Screen.height - Input.mousePosition.y);
This doesn't seem to be working in Web Player. Does anyone know how to fix it there?
@Garnager - you should open a new question and include the particulars. This answer is for a Rect in GUI coordinates, not Screen coordinates.
Answer by Eric5h5 · Aug 21, 2012 at 08:47 PM
If you're talking about GUI coordinates, don't use Input.mousePosition. That's for screen coordinates. Use Event.current.mousePosition instead.
Using Event.current.mousePosition creates a null referance exception. I tried the new keyword to no avail. $$anonymous$$aby Im implementing it wrong?
Ok that makes sense, however this test isnt an OnGUI call, While im mapping a gui box to the rect, the rect is a separate static declaration.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080003im_/https://answers.unity.com/themes/thub/images/avi.jpg)