- Home /
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.
Maybe this has to do with my camera angle/settings?
Problems:
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
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
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:
Software Cursor lags. This is placed in Update(), and the mouse will move faster than the software cursor (green circle).
How can I achieve a more accurate Mouse to WorldPosition?
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.
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.
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.
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.
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.
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.
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.