Last active
December 15, 2015 21:49
-
-
Save tzamora/5328656 to your computer and use it in GitHub Desktop.
OnControllerColliderHit CharacterController physics con rigidbodies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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