- Home /
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?
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.
Thanks for pointing out the Vector3.ToString rounding that occurs. I didn't believe you at first, but this is totally true, and was throwing my Debug.Log testing off.
"Vector3.ToString" actually has an overload which takes a number format string which will be used for each component. If you want to see all digits usually vec.ToString("F8")
should be enough for most cases. For numbers around 1.0 you actually see the end of the mantissa.
Answer by Techsuport · Sep 22, 2011 at 04:40 AM
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
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.$$anonymous$$ousePosition 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.)
I really don't see how this could have any relevance to grayfox's question.
Answer by allesisda · Apr 06, 2012 at 05:58 PM
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 -.-,
Don't use an answer to someone else's question to ask a question. The original question was wrong anyway - ScreenToViewportPoint is not inaccurate.
Answer by herrindiho · Nov 24, 2021 at 01:35 AM
Everybody seems to not be answering so i will. I'm assuming you're using Debug.Log to get your measurements. Debug.Log only returns up to the first decimal. What you need to do is Debug.Log(Camera.main.ScreenToViewportPoint(Input.mousePosition).y.ToString("f3"));
This will return 3 decimal places.
This works for anything that returns float values or more specifically a Vector3
Your answer
![](https://koobas.hobune.stream/wayback/20220612221407im_/https://answers.unity.com/themes/thub/images/avi.jpg)