- Home /
The CapsuleCast is broken.
void CheckGround()
{
Vector3 p1 = transform.position + Vector3.up * capsule.radius;
Vector3 p2 = transform.position + Vector3.up * (capsule.center.y - (capsule.height * 0.5f) + (capsule.radius * 3));
float groundCheckDistance = p2.y - p1.y;
p1.y += groundCheckDistance;
p2.y += groundCheckDistance;
if (Physics.CapsuleCast(p1, p2, capsule.radius, Vector3.down, out hitInfo, groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
Points p1 and p2 are initially correct, but the capsulecast appears below the desired position. Next, I have to add the height of the groundCheckDistance to the height of the points so that they rise to the correct height. But now they have the wrong values, nevertheless, giving the desired result. I don't understand how this works and why I need to do + = groundCheckDistance to each point in order for it to work.
The question is how to find the correct positions initially and get rid of a hack like this:
p1.y + = groundCheckDistance;
p2.y + = groundCheckDistance;
Second question: the documentation says: "The capsule is defined by the two spheres with radius around point1 and point2, which form the two ends of the capsule.". Following the instructions, this is how I find the first point:
Vector3 p1 = transform.position + Vector3.up * capsule.radius;
Position + upward offset by capsule radius value.
According to the instructions that the capsulecast is defined by two centers of the spheres, finding the second point should look like this:
Vector3 p2 = transform.position + Vector3.up * (capsule.center.y - (capsule.height * 0.5f) + capsule.radius);
But it doesn't work, although it should work. For some obscure reason, you need to add capsule.radius not once, but three times:
Vector3 p2 = transform.position + Vector3.up * (capsule.center.y - (capsule.height * 0.5f) + (capsule.radius * 3));
I have to add not one capsule.radius, but (capsule.radius 3). Why is that? Finally, the center of the second sphere must be offset as the center of the capsule minus half the height of the capsule plus the radius of the capsule, but not three of the radius of the capsule. In general, the capsule cast is very confusing.
I want like in the picture:
Your answer
Follow this Question
Related Questions
Physics.CapsuleCast Help 1 Answer
Why does CapsuleCast appear in the wrong place? 0 Answers
How to find the center of mass from capsulecast 0 Answers