On my player, I have decided to eschew the heavy character controller for a rigidbody and collider. In the past, I have used the the character controller and collision flags returned from the move method of the controller to determine whether the player is grounded, like this:
this.moveCollisions = this.charControl.Move(this.moveDir * Time.deltaTime);
this.isGrounded = (this.moveCollisions & CollisionFlags.CollidedBelow) != 0;
Now that I am using a rigidbody and collider only, my solution for detecting whether the player is grounded is being done like this:
(from the player controller script)
public function OnCollisionEnter(collisionInfo : Collision) {
var anyGrounded: boolean;
anyGrounded = false;
for (var contact : ContactPoint in collisionInfo.contacts) {
if (Mathf.Approximately(contact.normal.y, 1)) {
anyGrounded = true;
}
}
if (anyGrounded) {
this.isGrounded = true;
}
// Do other collision processing....
return;
}
public function OnCollisionExit(collisionInfo : Collision) {
var anyGrounded: boolean;
anyGrounded = false;
for (var contact : ContactPoint in collisionInfo.contacts) {
if (Mathf.Approximately(contact.normal.y, 1)) {
anyGrounded = true;
}
}
if (anyGrounded) {
this.isGrounded = false;
}
// Do other collision processing....
return;
}
This seems to work well, but I am curious if there are unforeseen issues with the approach, and also if there is a better way.
Thanks for your help. I thought about a ray cast solution a bit as well.
– ChazBass