- Home /
Input Touch.position 'inverted' relative to screen?
Sorry if this has been covered elsewhere, I did not see it.
Is the Input's Touch position 'inverted' relative to the screen? The reason I ask is that if I do this:
var touch = Input.GetTouch(0);
if (windowRect.Contains(touch0.position))
{
.. do something ..
}
where windowRect is set in OnGUI:
windowRect = GUILayout.Window(GetInstanceID(), windowRect, DisplayData, "title");
It will only 'do something' IFF I touch the screen in the 'mirrored' position (up/down aka Y direction). Ex: if my windows is near the top of my screen, it will not detect this containment if I touch near the top (inside the window). But it WILL detect if I touch on the opposite side of the screen (same X, opposite Y).
I must have missed something in the docs? Or is it a bug?
Update: Is this what GUIUtility.ScreenToGUIPoint is for?
Yoda, I can't believe you've been stumped by this same invertedness !
Answer by Dreamblur · Jun 23, 2011 at 12:41 AM
Technically, it is not inverted. Touch position and mouse position are returned by the Input class in screenspace coordinates, whereas OnGUI uses GUI space coordinates. In screenspace, (0, 0) is on the bottom-left corner of the screen while it is on the top-left corner of the screen in GUI space.
$$anonymous$$y puzzlement is that this same check for $$anonymous$$ouse coordinates seems to be ok. At least, I haven't noticed any problems. But Touch coordinates seem backward. I tried copying the windowRect and setting y to Screen.height-windowRect.y, but that didn't help. I'm not sure if these GUIUtility funcs will help, nor which one (ScreenToGUI or GUIToScreen) would be better. Trying them now...
To convert screenspace coordinates to GUI space coordinates:
new Vector2(screenspace.x, Screen.height - screenspace.y) // where screenspace is the Vector2 in screenspace coordinates
Yep, that's pretty much I just came up with too. Thanks. I think the difference is (and btw the GUIUtility funcs didn't work) because I was doing this in Update and not OnGUI.