x


ScreenToViewportPoint not precise enough

I'm doing some basic GUI manipulation with the mouse and I'm getting some weird behavior like the GUI element I'm moving not being exactly where the mouse pointer is. I'm using ScreenToViewportPoint to convert the mouse position to viewport space and I'm finding that it's not precise enough. It only returns values to like 0.3 instead of 0.332, for example. If there any way to get more floating point accuracy?

more ▼

asked Sep 21 '11 at 03:08 PM

grayfox gravatar image

grayfox
71 7 9 11

What makes you believe it only returns numbers like 0.3? If that was the case, there would only be 10 positions across the screen. Note that Vector3.ToString only shows an approximate value, so if your belief comes from using Debug.Log or print, it is just those functions fooling you.

Sep 22 '11 at 03:38 AM Waz
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i was doing stuff with mouse and GUI stuff a while ago, found that some somethings convert stuff to different orientations. specifically inverting the y value, the origin will either be in the top left or bottom right, so try just multiplying a Y value by -1

However I cant remember off the top of my head what does the orientation in the bottom right, but i think the GUI stuff uses top left most the time

more ▼

answered Sep 22 '11 at 04:40 AM

Techsuport gravatar image

Techsuport
81 6 6 10

The problem is that standard microsoft C# pixel-based locations are backwards. Y=0 is on TOP, with positive Y going down. Code-based guis -- GUI.Label( new Rectangle...) -- use that backwards system, to make the C# drawing routines happy (also for javascript -- uses the same routines.)

Viewport (0-1) puts Y=0 on the bottom and positive Y going up, like normal. Likewise Input.MousePosition uses Y=0 on bottom with pixels going up (also normal.) I believe that ViewPortToScreen gives you the "good" system, with y=0 on the bottom, like it should be, and ScreenToViewPort assumes Y=0 is the bottom with Y going up.

The fix, to go from "correct" Y-bottom screen to "Y=0 top and plus Y goes down", is to use Screen.height-Y (not Y times -1 -- negative coords are off the screen either way.)

Sep 23 '11 at 05:01 PM Owen Reynolds

I really don't see how this could have any relevance to grayfox's question.

Sep 23 '11 at 09:41 PM Waz
(comments are locked)
10|3000 characters needed characters left

i am also having problems with unprecize mouseposition -.- i want my player to shoot where the mouse is, but a lot of times it shoots wrong cause of this... and i dont really want to scale my hole world by 10 or 20 -.-,

more ▼

answered Apr 06 '12 at 05:58 PM

allesisda gravatar image

allesisda
8 3 4 11

Don't use an answer to someone else's question to ask a question. The original question was wrong anyway - ScreenToViewportPoint is not inaccurate.

Apr 06 '12 at 09:05 PM Waz
(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:

x3131
x495
x291
x3

asked: Sep 21 '11 at 03:08 PM

Seen: 801 times

Last Updated: Apr 06 '12 at 09:05 PM