- Home /
CapsuleCast colliding improperly
The Physics.CapsuleCast feature seems to be returning incorrect results.
For example:
RaycastHit hit;
Vector3 p1 = transform.position + Vector3.up * 2;
Vector3 p2 = p1 + Vector3.up * 3;
if (Physics.CapsuleCast(p1, p2, capsuleRadius, transform.forward, out hit, 10,mask.value)) {
if(hit.point.y < p1.y)
Debug.Log("Error!");
}
The above code creates a capsule between p1 and p2 and casts it "forward". In this case p1 and p2 form a vertical capsule. And forward is therefore perpendicular to that. In no case, therefore, should a hit result in a Y point that is below p1. A vertical capsule casting horizontally could not possibly return a hit location lower than the capsule itself.
Yet it often does. :/
Is this a bug in Unity?
Answer by aldonaletto · Oct 06, 2011 at 11:04 PM
I found a bug in the capsule collider that was studied more deeply by @Bunny83, and the whole thing is in the question The (ab)normal mystery. It seems the bug has not been fixed yet (despite it was reported to Unity then), and maybe it affects CapsuleCast as well - we didn't test it.
EDITED: I read your question more carefully, and it seems that you're not taking in account the capsule radius - according to the docs, p1 and p2 are the centers of the upper and lower spheres that compose your capsule, so the capsule extends from p1.y-radius to p2.y+radius. The bug mentioned above affects the normals calculated when the capsule collider is rotated around more than one axis.
Indeed that does look like borked capsule collision. I will point out that PhysX itself doesn't have this problem inherently, so it's gotta be in the Unity implementation.
Not sure if it's related to this or not. But will try a different sort of collider.
I've just edited my answer because I believe to had found the problem. Take a look at that.
Answer by Owen-Reynolds · Oct 07, 2011 at 03:08 PM
This assumption: "...form a vertical capsule. And forward is therefore perpendicular to that." (italics mine) isn't correct.
Sure, since you use Vector3.up
to build the two points, the capsule is guaranteed to be world-verticle. But, direction transform.forward
still means what it always does -- your personal local forward vector (nothing to do with the capsule that will be constructed.)
If you're casting from a standard non-tilting charControlled object, then transform.forward is always flat, so the assumption is coincidentally correct, but for a rolling ball, say, you're casting in a random direction.
Your answer
Follow this Question
Related Questions
IgnoreCollision affecting children & unrelated objects when it should'nt ? 0 Answers
Bug? ( failed collision from golf simulation ) 3 Answers
No bounce after rigidbody collision 2 Answers
AddForce to a point of a GameObject, not the center 1 Answer
Camera gets flung off of the map when the player collides with certain objects. 0 Answers