Skip to content

Instantly share code, notes, and snippets.

@tzamora
Last active December 15, 2015 21:49
Show Gist options
  • Save tzamora/5328656 to your computer and use it in GitHub Desktop.
Save tzamora/5328656 to your computer and use it in GitHub Desktop.
OnControllerColliderHit CharacterController physics con rigidbodies
public float pushPower = 2.0f;
public float weight = 6.0f;
//
// push rigidbodies
//
void OnControllerColliderHit (ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
Vector3 force;
// no rigidbody
if (body == null || body.isKinematic) { return; }
// We use gravity and weight to push things down, we use
// our velocity and push power to push things other directions
if (hit.moveDirection.y < -0.3f)
{
force = (new Vector3 (0f, -0.5f, 0f)) * gravityConstant * weight;
}
else
{
force = hit.controller.velocity * pushPower;
}
// Apply the push
body.AddForceAtPosition(force, hit.point);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment