x


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?

more ▼

asked Jun 23 '11 at 12:35 AM

DaveA gravatar image

DaveA
26.8k 153 171 257

Yoda, I can't believe you've been stumped by this same invertedness !

Mar 27 '12 at 05:55 AM ina2

We were all new once....

Mar 28 '12 at 02:41 AM DaveA
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 23 '11 at 12:41 AM

Dreamblur gravatar image

Dreamblur
703 9 16 22

My puzzlement is that this same check for Mouse 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...

Jun 23 '11 at 12:46 AM DaveA

To convert screenspace coordinates to GUI space coordinates:

 new Vector2(screenspace.x, Screen.height - screenspace.y) // where screenspace is the Vector2 in screenspace coordinates
Jun 23 '11 at 12:59 AM Dreamblur

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.

Jun 23 '11 at 01:03 AM DaveA
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x980
x915
x606
x501

asked: Jun 23 '11 at 12:35 AM

Seen: 4095 times

Last Updated: Mar 28 '12 at 02:41 AM