- Home /
Boxcast returns one incorrect hit point of (0,0,0).
I have a box collider, and to test if it is near the ground, I use a box cast:
RaycastHit[] raycastHits = Physics.BoxCastAll(boxCollider.gameObject.transform.TransformPoint(boxCollider.center), boxCollider.size * 0.5f, Vector3.down, boxCollider.gameObject.transform.rotation, 0.3f);
Originally I was using BoxCastNonAlloc, but I switched to the above to debug, because I feared it was something weird with the NonAlloc functionality. They should be the effectively the same, and they did return the same results:
int hits = Physics.BoxCastNonAlloc(boxCollider.gameObject.transform.TransformPoint(boxCollider.center), boxCollider.size * 0.5f, Vector3.down, raycastHits, boxCollider.gameObject.transform.rotation, 0.3f);
I have confirmed that that the cast is going where I expect it to go with some debug logging and draw lines.
It is correctly registering hits, however, it seems that the first hit always returns an incorrect point: (0, 0, 0). The normal usually seems to be correct, but hard to confirm given that it's being called every update. So, essentially, if I hit anything, I always get at least one RaycastHit where the point is (0, 0, 0), and the normal is (0, 1, 0), presumably because it is hitting the floor.
Example outputs of hits, in point then normal order:
One incorrect, two correct:
3: (0.0, 6.0, -21.5) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0), (0.0, 6.2, -21.4) (0.0, 1.0, 0.0),
One incorrect, one correct:
2: (-0.7, 6.0, -21.1) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0),
All of them incorrect:
2: (0.0, 0.0, 0.0) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0),
It is correct in the number of hits, just the data that is returned is incorrect at least one time in every array of results returned.
Just for additional information, here is the code used to print out the hits. "hits" is equal to the length of the array returned for BoxcastAll, or equal to the value returned from BoxCastNonAlloc:
string message = hits.ToString() + ": ";
for (int i = 0; i < hits; ++i)
{
message += raycastHits[i].point + " " + raycastHits[i].normal + ", ";
Debug.DrawRay(raycastHits[i].point, raycastHits[i].normal, Color.red, 1f);
}
Debug.Log(message);
I am using the latest beta of Unity 5.4, beta 20. I checked the history there and didn't see anything regarding incorrect RaycastHit results with Boxcast.
I too have stumbled into this problem. Pretty much a year later after you posted this. Hopefully this is addressed soon!
Answer by McGravity · Jun 18, 2017 at 07:55 AM
There is a note under the entry in the documentation which I guess explains the behaviour:
https://docs.unity3d.com/ScriptReference/Physics.BoxCastAll.html
... and the zero vector gets returned in RaycastHit.point. ...
Your BoxCast hits the BoxCollider of your Player at the start of the sweep.