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.

I raycast to terrain composed of a box collider

88678-groundplane.png

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?

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.

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.