- Home /
Why does CapsuleCast appear in the wrong place?
There is a empty object with a capsule collider. The capsule collider is raised relative to the object's transform position. I want to create a CapsuleCast between the bottom edge of the collider and the bottom point of the object (transform position). That is, so that the CapsuleCast would be a continuation of the capsule collider, reaching the zero coordinate of the object.
public CapsuleCollider capsule;
private void FixedUpdate()
{
CheckGround();
}
bool isGrounded = false;
RaycastHit hitInfo;
void CheckGround()
{
Vector3 p1 = transform.position + Vector3.up * capsule.radius;
Vector3 p2 = transform.position + Vector3.up * (capsule.center.y - (capsule.height / 2) + capsule.radius);
float groundCheckDistance = p2.y - p1.y;
if (Physics.CapsuleCast(p1, p2, capsule.radius, Vector3.down, out hitInfo, groundCheckDistance, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
For some reason, the capsule is formed under the transform position and goes in the opposite direction: down. If i change the direction Vector3.down to Vector3.up, then the CapsuleCast appears above the upper border of the capsule collider, much higher than the desired one.
How to create CapsuleCast in the right place like in the picture?
Your answer
Follow this Question
Related Questions
Physics.CapsuleCast Help 1 Answer
The CapsuleCast is broken. 0 Answers
CapsuleCast consistent error in size 1 Answer
Physics.SphereCastAll with triggers 1 Answer
Plane Mesh Collider doesn't stop Capsule ... sometimes. 1 Answer