Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by CarterG81 · Feb 23, 2017 at 05:28 PM · raycastmousemousepositionscreentoworldpoint

How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow?

Update: The Y-Axis works perfectly. It is only one the X axis that a "big leap" problem occurs.

Alt text

Maybe this has to do with my camera angle/settings?

Alt text


Problems: Alt text Alt text

Green Circle = 3D sphere collider (represented by 2D sprite green circle, billboarded to the camera.)

So the Green Circle is a 3D object placed exactly on the ground in the 3D worldspace.

alt text

I raycast to terrain composed of a box collider

alt text

My code is pretty standard.

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if(Physics.Raycast(ray, out hit, 10000, LayerMask.GetMask("Ground")))
         {
             Debug.DrawRay(ray.origin, ray.direction * 10000, Color.green, 3);
             myCursorObject.transform.position = hit.point;
         }

This is a 3D world with flat terrain (box colliders = "Ground" layer) The camera is at an angle looking down from a distance, with an X rotation of 60 (isometric view).

I have two problems

  1. Mouse Position doesn't always translate to world position. As you can see in the above image, I can move the mouse and it will NOT follow the object. There are random amounts the mouse has to move before the image also moves. It seems random. Sometimes it has big leaps in world position; sometimes it updates more accurately. Here's the big leap: Alt text

  2. Software Cursor lags. This is placed in Update(), and the mouse will move faster than the software cursor (green circle). Alt text

How can I achieve a more accurate Mouse to WorldPosition?

groundplane.png (126.5 kB)
3d-green-cursor.png (172.1 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by CarterG81 · Feb 24, 2017 at 06:49 PM

I was (possibly) wrong about the answer to this. Seemed to be a problem with the Near Clipping. Doesn't help that I don't understand Camera Settings very well.

https://forum.unity3d.com/threads/increasingly-inaccurate-results-of-screenpointtoray-as-transform-size-increase-choppy-results.459818/

Near Clipping on Camera was set to 0.1 Changing it to any other value (1, 10, 100) fixed this.



Floating Point Limitation & ScreenPointToRay()

The inaccuracy was in the fact that Unity's float limitations when combined with the ScreenPointToRay method. This limitation destroyed the calculations, resulting in "jumping".

My world position was in the 5-digits. Character started at -32000, -3000.

This explains why the Y-Axis worked but the X-Axis failed to calculate properly.

I set my playercharacter to position 0,0, and it worked flawlessly.

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 CarterG81 · Feb 26, 2017 at 11:18 PM 0
Share

This poses yet another problem in Unity's floating point limitations.

This means that if your game has mouse (screen) to object (worldspace) calculations, you have even less space to work with for your world.

This is incredibly annoying, and honestly should a priority since a lot of games run into problems with transforms. The more I learn of Unity, the less I would ever refer anyone to it as an engine. Especially since it is closed source, plagued with performance woes, and severely limited in some of the dumbest ways.

avatar image
1

Answer by Bunny83 · Feb 23, 2017 at 05:51 PM

Your first problem is pretty simple: Your green circle seem to have a sphere collider. This sphere collider prevents your ray from reaching the terrain collider below. So you most likely use the same "Ground" layer for that sphere collider.

The hardware cursor moves and renders independent from Unity's visual update. There's no way to improve that. Just ensure you have a high framerate.

Comment
Add comment · Show 3 · 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 Bunny83 · Feb 23, 2017 at 05:54 PM 0
Share

ps: In your image it's not the hardware cursor that lags behind but your software cursor. The "hardware" cursor is your arrow that is drawn by your OS.

avatar image CarterG81 · Feb 24, 2017 at 05:59 PM 0
Share

Ah thank you for that correction; I meant software the whole time, but since I personally demonize software cursors in games, my vocabular is strictly "Hardware or Hardware?" so I misspoke lol.

The sphere collider blocking the ray is unlikely.

avatar image CarterG81 · Feb 24, 2017 at 06:05 PM 0
Share

As I thought, the sphere collider uses a default layer, and turning it off (and all other colliders that might be interfering) and it still has the same "big leap" effect. It didn't make sense that it would be blocking, seeing as how I don't use the ground layer for anything but the ground.

However on the Y axis, it moves exactly as it should. It's only on the X axis that it has a "big leap". I updated with an image showing the successful Y-Axis movement.

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

88 People are following this question.

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

Related Questions

Mouse plane does not detect height 1 Answer

Get Vector3 of Position Clicked on Plane 1 Answer

Camera.main.ScreenToWorldPoint works within Unity editor, but not in builds 0 Answers

How to prevent GameObject from spawning on top of each other? 1 Answer

How to Limit Input.mousePosition or Raycast 1 Answer


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