Question by
Kaskorian · Jul 28, 2017 at 08:45 AM ·
collisiondetectiongroundedcapsulecollider
How to fix grounded bug of capsule collider?
Hey there,
I am trying to detect the collision at the bottom of a capsule collider and it is already working. But the detection is just called at the OnCollisionEnter()
private void OnCollisionEnter(Collision collision)
{
if (collision.contacts.Length > 0)
{
ContactPoint contact = collision.contacts[0];
if (Vector3.Dot(contact.normal, Vector3.up) > 0.5)
{
grounded = true;
anim.SetBool("grounded", true);
}
else
{
grounded = false;
anim.SetBool("grounded", false);
}
}
}
In OnCollisionExit() I set the grounded variable to false
private void OnCollisionExit(Collision collision)
{
grounded = false;
}
But here is the Problem. Because I have a jumping function in the game I have to make sure that my capsule collider fits the current height of my character. This is the function to handle this:
private void FitCollider()
{
heighestPos = head.position.y + 0.04f;
lowestPos = Mathf.Min(leftFoot.position.y, rightFoot.position.y);
deltaCollider = heighestPos - lowestPos;
capCol.height = deltaCollider;
capCol.center = new Vector3(0, deltaCollider / 2 + (lowestPos - transform.position.y), 0);
}
But when my character is moving or is doing any other Action the Position of the bones is changing and also the height and Position of the collider and thats why there is sometimes a Enter or Exit detection where actually no one should occur.
How can I fix this Problem
Looking forward for a Reply :)
Comment