- Home /
Boxcast vs Raycast Oddities
Weird issue I'm having :
I'm firing a 1 meter large boxcast DOWN towards the ground. It starts 1 meter off the ground and can at a maximum go down 1 meter. (so, it should have a hit distance of 1). The issue is that the boxcast says the hit distance is .9975, not 1.
A raycast starting at the bottom of the box going the same maximum distance of 1 meter registers a hit distance of 1.
That makes make me think that the boxcast is starting .0025 meters too low, but the hit.point.y value always equates the raycast even when the distance differs.
In this example, the box/raycast is only .701 meters off the ground. The hit.point.y of both is equal, but the hit.distance differs. Any idea what is going on? Is it just an oversight by me? Thanks
Boxcast is RED ▉ Raycast is GREEN |
void collision()
{
RaycastHit boxhit, rayhit;
Physics.Raycast(new Vector3(0f, 1f, 0f), Vector3.down, out rayhit, 1f);
Physics.BoxCast(new Vector3(0f, 1.5f, 0f), new Vector3(.5f, .5f, .5f), Vector3.down, out boxhit, Quaternion.identity, 1f);
Debug.DrawRay(new Vector3(boxhit.point.x, 1f, boxhit.point.z), Vector3.down*boxhit.distance, Color.red);
Debug.DrawRay(new Vector3(0f, 1f, 0f), Vector3.down*rayhit.distance, Color.green);
Debug.Log("boxhit.point.y: " + boxhit.point.y + " boxhit.distance: " + boxhit.distance);
Debug.Log("rayhit.point.y: " + rayhit.point.y + " rayhit.distance: " + rayhit.distance);
}
UPDATE:
I even put in the Physics.Boxcast demo script (which was broken at first, using full size instead of halfExtents). And placing a boxes faces 1 meter apart from each other gives a hit distance of .9975. Is this a bug? So confused.
void FixedUpdate()
{
//Test to see if there is a hit using a BoxCast
//Calculate using the center of the GameObject's Collider(could also just use the GameObject's position), half the GameObject's size, the direction, the GameObject's rotation, and the maximum distance as variables.
//Also fetch the hit data
m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, transform.localScale *1/2, transform.forward, out m_Hit, transform.rotation, 1f);
if (m_HitDetect)
{
//Output the name of the Collider your Box hit
Debug.Log("Hit : " + m_Hit.distance);
}
}
I think that's just how it works. It seems as if boxCast (only) distance is always short by 0.025, no matter what.
I really wanted to say there's a problem with your set-up. But I just tried my own test, and sure enough, got correct hit position (rounding error of 0.00000001), but distance is short by 0.0025. $$anonymous$$oved the cast into Update to let the target "settle down" but same thing. $$anonymous$$oved the box sideways, so i wasn't hitting the exact center -- same thing. Changed to a sphereCast -- the distance is now perfect. Only boxCasts have the problem.
One thing I just finally figured out is that (at least in 2019.4.22 LTS), a zero thickness collider being hit will give the right hit.distance and hit.collider, but the hit.point will always be identical to the starting point of the BoxCast. If you use Raycast with the exact same (applicable) parameters, it will work fine. Just thought I'd leave this here for posterity.
Answer by MelvMay · Jul 06, 2019 at 07:18 AM
Both 2D (Box2D) and 3D (PhysX) use a "contact offset" which provides a buffer zone for collision-detection. This provides a region where a contact is considered touching and is used to provide a stable col-det simulation therefore they return where the collision would happen during normal simulation conditions. Look at positions during normal simulation; objects are not exactly aligned nor do they need to be apart from some sense of "precision".
Raycasts are not used during standard simulation and are there for miscellaneous use. They are calculate as the exact intersection.
$$anonymous$$y Physics.defaultContactOffset is .01 (which divided by 4 is .0025 i guess). Does this still make sense? I can't relate these two numbers confidently. I'd like to have precision like a raycast but just in a box-like volume if possible.
I suppose you could test by changing that default to 0.2 (or anything relatively huge). See if it does anything. $$anonymous$$y feeling is *casts use exact math. The interpenetration a fudge factor for rigidbodies, to avoid twitching.
Yeah setting it to 20 doesn't do anything, but the exactness of the number makes it seem like some offset but I don't know what it is called.
Your answer
Follow this Question
Related Questions
RaycastHit always returns 0 1 Answer
Problem With Raycasthit Angle 0 Answers
Raycast detects box collider, but not capsule collider 0 Answers