- Home /
Raycast behavies as if its snapping to grid on standalone build
I am using raycast to locate a GameObject in worldspace i.e. I am casting a ray from my UI camera and taking its hit.point as the new location for my object.
This works nice and smooth when ran in the editor however when I build and test in stand alone the object moves as if its snapping to a grid.
I set up a few messages to see the results of the case; in editor I see [Note gradual change to the y value]
Mouse @749,949 cursor move to 9992.822,10070.4,10108.08
Mouse @751,950 cursor move to 9993.35,10070.61,10108.08
Mouse @754,952 cursor move to 9993.983,10070.93,10108.08
in build I see [Note the 3+/- unit change in the y value]
Mouse @887,993 cursor move to 9994.194,10054.46,10108.08
Mouse @887,994 cursor move to 9994.194,10057.95,10108.08
Mouse @887,995 cursor move to 9994.194,10057.95,10108.08
Mouse @887,996 cursor move to 9994.194,10057.95,10108.08
Mouse @887,997 cursor move to 9994.194,10057.95,10108.08
Mouse @887,998 cursor move to 9994.194,10057.95,10108.08
Mouse @887,999 cursor move to 9994.194,10057.95,10108.08
Mouse @887,1000 cursor move to 9994.194,10057.95,10108.08
Mouse @887,1001 cursor move to 9994.194,10057.95,10108.08
Mouse @887,1002 cursor move to 9994.194,10057.95,10108.08
Note from mouse.y 993 to 994 the world pos moved 3.9 units; from 995 to 1002 it didnt move at all. The plane I am aiming at is 109 units out from the camera and for this test level with the camera such that the object should move up and down, left and right but not in and out.
Code in question
Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, CursorLayers))
{
CursorPosition.transform.position = hit.point;
Debug.Log("Mouse @" + Input.mousePosition.x.ToString() + "," + Input.mousePosition.y.ToString() + " cursor move to " + hit.point.x.ToString() + "," + hit.point.y.ToString() + "," + hit.point.z.ToString());
}
I have tested the build at a number of diffrent resolutions the exsact results are diffrent but always jumps by a couple of units while the in editor run always moves smooth.
I'll keep digging on it my self but any pointers or tips would be much appreciated.
Thanks
Had a hard time getting that to post; looks like some of the formatting got trashed; let me know if you need more info
Ran another test so I could visualize the hit.point locations; in this test I generate a cube with no collision mesh at the hit.point. I am calling 1000 ray casts via an invoked method to fire the 1000 ray casts each 1 pixel right from the last at 500 pixels up the screen; the result should be a solid line accross the screen and that is what we get in the editor however at run time we get a dashed line.
In editor 
In stand alone build 
@meat5000 Thanks for the clean up on the formating thing was giving me a hard time.
Answer by lodendsg · Sep 15, 2013 at 02:32 PM
Found the answer;
The casue of this behaviour is the near clip; I had set my GUI camera near to 0.01f; did this so that objects passing through the screen wouldnt get cliped till the last second draw back is that rays are cast from the near through the pixel location of the mouse and apparently to only 2 points of perision which causes them to hop on a grid that is roughly 100 by 100 (I'm guessing) when the near is set to 0.01;
I adjusted my near to be as far out as I can tollerate for this camera and everything smoothed right out. Now why this causes the in accuracy in the ray cast in the build but not the editor I'm not sure.
For note this also cleaned up an accuracy issue I had where in my 3D GUI I have a couple of render to texture cameras and I am passing my ray cast hit texture cords and casting a second ray on the render to text camera which also had the near clip set to 0.01f.
The default of 0.3f works moderetly Im guessing this gives a grid of 300 by 300 when I adjust to 3.0f I get nice smooth behaviour above 5 and accuracy starts to fall again below 1 also isnt so smooth.
To see exsactly whats going on I used DrawRay; zoom in or set your near clip low and you will see it bounce around.
Your answer