Hello,
I’m trying to get an object to follow my mouse position within the scene, and to do that I’m using ScreenPointToRay. When I run the scene, the object doesn’t follow my mouse, and I get a warning saying: Input position is { Infinity, -1… }.
Also, on the actual object thats suppose to be following my mouse, I get a little warning displaying under the Transform in my Inspector window saying: Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range
What does any of this mean, and how would I go about fixing it?
Here is my script:
// -- place control -- \\
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
Physics.Raycast(ray, hit, Mathf.Infinity);
var positionForm = Vector3(hit.point.x, transform.position.y, hit.point.z) - transform.position;
transform.Translate(positionForm);
Thanks
Hm… I notice a couple of things in your code.
-
transform.Translate moves your object by the vector you input - not to it. Just call transform.position = positionForm; to move it to the hit point.
-
You’re not testing if the ray cast hits something. Physics.Raycast returns a boolean that indicates whether the raycast actually hit anything. I’m not sure what hit.point is set to if the ray cast doesn’t hit anything, but I’d imagine based on the error you’re seeing, it’s infinity.
-
Floats have to fit into a certain number of bytes, and so they lose precision as the number they represent gets larger. Since you’re trying to place an object at such a high value, the floats are imprecise(perhaps someone more intelligent than me could expand on this oversimplification).
if(Physics.Raycast(ray, hit, Mathf.Infinity){
transform.position = Vector3(hit.point.x, transform.position.y, hit.point.z);
} else {
transform.position = ray.GetPoint(10f); //returns a point ten units out along the ray
}