- Home /
rect.Contains problem?
Hello, I'am trying to make something happen when the mousePosition is NOT over a rect (actually it should be the touch position, because it's for an Android app) :)
Here's my code which is in the Update void:
Rect purchaseButtonRect = new Rect( Screen.width - purchaseButtonWidth, Screen.height - purchaseButtonHeight, purchaseButtonWidth, purchaseButtonHeight);
if (!purchaseButtonRect.Contains(Input.mousePosition)) {
print("The touch is not over the rect");
}
The problem is, that it prints the text even if my finger is pressing on the button (using the Unity Remote)
How can I fix that?
Thanks, Andreas :)
Answer by Jamora · Jun 01, 2013 at 01:32 PM
Since you are using touches, then checking for mouse position is the wrong way to go. Use Input.GetTouch().position
instead.
Answer by saschandroid · Jun 01, 2013 at 01:28 PM
Reading the scripting reference you can see that the origin of a (gui) Rect is top left, mousePosition bottom left.
If your mouse is in the bottom right corner (where your button is), mousePosition will give you something around (Screen.width, 0), but your button position is at (around) (Screen.width, Screen.height).
If I am right, your funtion should print if the mouse position is at top right.
if (!purchaseButtonRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePostion.y))
Yes, that's because it would make more sense to use the GUI mouse position: Event.current.mousePosition in OnGUI inside a mouse event
Your answer
Follow this Question
Related Questions
Best way to check where touched 1 Answer
How to Touch Drag 3D Objects 2 Answers
android touch not detected. 1 Answer
Jump when touch the screen Android (GAME MADE FOR PC) 5 Answers
touch GUITexture Help 1 Answer