Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by grayfox · Sep 21, 2011 at 03:08 PM · cameraguitexturedrag

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Sep 22, 2011 at 03:38 AM 2
Share

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.

avatar image skullthug Waz · Jan 16, 2020 at 09:28 PM 0
Share

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.

avatar image Bunny83 skullthug · Jan 16, 2020 at 09:52 PM 1
Share

"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.

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Sep 23, 2011 at 05:01 PM 0
Share

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.)

avatar image Waz · Sep 23, 2011 at 09:41 PM 0
Share

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

avatar image
-1

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 -.-,

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Apr 06, 2012 at 09:05 PM 0
Share

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

avatar image
0

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

@grayfox

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

To display GUI Texture only on main camera 1 Answer

Can I have a prefab appear on my screen like a gui texture does? 1 Answer

How to display multiple textures over the camera? 1 Answer

Making GUI Buttons on a GUI Texture 1 Answer

camera orbit on mouse drag 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges