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) :slight_smile:

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 :slight_smile:

Since you are using touches, then checking for mouse position is the wrong way to go. Use Input.GetTouch().position instead.

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))