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?
3 Answers
3Everybody 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
@grayfox
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
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.
– Waz
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.
– Waz