Question by
effekt · Jan 25, 2017 at 07:06 PM ·
c#collisioncharacterisgrounded
CapsuleCollider check if grounded
So i have a problem checking if my character is grounded, and it is tied to the position of the capsule collider, I just started unity so I'm not sure what I did wrong. The problem is that the spherecast returns false, like it didn't hit anything.
Here are some images of my character/capsuleCollider to better illustrate.
If the collider sticks out of the character as shown on the first picture, it works, but my character is floating 50cm in the air.
Here is the code showing how i detect if im grounded. Keep in mind I'm working with a rigidbody and not a charactercontroller.
bool IsGrounded()
{
Vector3 p1 = transform.position;
float distanceToObstacle = 0;
RaycastHit hit;
// Cast a sphere wrapping character controller 0.1 meter down to check if it hits anything
Collider collider = GetComponent<Collider>();
if (Physics.SphereCast(p1, collider.bounds.size.x / 2, new Vector3(0,-10,0), out hit))
{
distanceToObstacle = hit.distance;
if (distanceToObstacle < 0.1f)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Comment
Answer by IAmCoolAndAwesome · Jun 28, 2020 at 11:39 PM
Thanks that totally helped. I was looking for an answer and that was it. It was the only one so far @effekt