Unity - Mecanim & Rigidbody on Third Person Controller - Gravity bug?
Posted
by
Celtc
on Game Development
See other posts from Game Development
or by Celtc
Published on 2014-08-13T22:10:12Z
Indexed on
2014/08/21
4:34 UTC
Read the original article
Hit count: 728
I'm working on a third person controller which uses physX to interact with the other objects (using the Rigidbody component) and Mecanim to animate the character.
All the animations used are baked to Y, and the movement on this axis is controlled by the gravity applied by the rigidbody component.
The configuration of the falling animation:
And the character components configuration:
Since the falling animation doesn't have root motion on XZ, I move the character on XZ by code. Like this:
// On the Ground
if (IsGrounded())
{
GroundedMovementMgm();
// Stores the velocity
velocityPreFalling = rigidbody.velocity;
}
// Mid-Air
else
{
// Continue the pre falling velocity
rigidbody.velocity = new Vector3(velocityPreFalling.x, rigidbody.velocity.y, velocityPreFalling.z);
}
The problem is that when the chracter starts falling and hit against a wall in mid air, it gets stuck to the wall. Here are some pics which explains the problems:
Hope someone can help me. Thanks and sory for my bad english!
PD.: I was asked for the IsGrounded() function, so I'm adding it:
void OnCollisionEnter(Collision collision)
{
if (!grounded)
TrackGrounded(collision);
}
void OnCollisionStay(Collision collision)
{
TrackGrounded(collision);
}
void OnCollisionExit()
{
grounded = false;
}
public bool IsGrounded()
{
return grounded;
}
private void TrackGrounded(Collision collision)
{
var maxHeight = capCollider.bounds.min.y + capCollider.radius * .9f;
foreach (var contact in collision.contacts)
{
if (contact.point.y < maxHeight && Vector3.Angle(contact.normal, Vector3.up) < maxSlopeAngle)
{
grounded = true;
break;
}
}
}
I'll also add a LINK to download the project if someone wants it.
© Game Development or respective owner