- Home /
Raycast gives wrong hit info when hit a plane
I'm trying to get the hit info of a plane with raycast but i only gets wrongs values.
The plane isn't rotated and its position is at (-68, 0, -17), but when I raycast it, you can see that the y coordinate of the hit point gives a weird number (6.003722E-17 instead of 0) and a weird normal vector ((0, 1, 2.220446E-17) instead of (0, 1, 0)).
The normal vector is always the same, but the y coordinate of hit point changes a little when i change x and z coordinates.
PD 1: Yes, I'm shooting the visible side of the plane.
PD 2: These values are too big to be an IEEE 754 approximation error (Mathf. Approximately returns false).
The code is very simple (I try to replace Update with FixedUpdate and nothing changes)
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
Debug.Log("Hit point: (" + hit.point.x + ", " + hit.point.y + ", " + hit.point.z + ")");
Debug.Log("Normal: (" + hit.normal.x + ", " + hit.normal.y + ", " + hit.normal.z + ")");
}
}
Answer by pauldarius98 · Feb 28, 2021 at 08:53 AM
There are some unity floating errors there (maybe?) but 2.220... E-17 is basically 0 ( is like 0.00000000000000000220). What you could do would be to make your own method of checking if a vector is the same with a given error
public bool SameVector(Vector3 a, Vector3 b, float allowedError)
{
return Mathf.Abs(a.x - b.x) < allowedError && Mathf.Abs(a.y - b.y) < allowedError && Mathf.Abs(a.z - b.z) < allowedError;
}
And then you can check the vectors like
var same = SameVector(a, b, 10E-10);