- Home /
Character controler OR rigidbody ?
Hi, I make some script based on character controler but I got a problem after lot of hours of development :/. I want my player can move mecanism with his mass (like rigidbody) but when I mount on my device (only physics device) I can't move the platform.
So, do I restart all with "rigidbody + collider" or try something with character controler. Or combine character controler + rigidbody but it's not a good solution...
Answer by aldonaletto · Feb 15, 2012 at 11:53 PM
You're right: mixing CharacterController and Rigidbody is really a bad idea (unless it's a kinematic rigidbody).
To simulate physical effects like pushing rigidbodies, you can use this:
var force = 5.0;
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.rigidbody){ // if the hit object is a rigidbody... hit.rigidbody.AddForce(-hit.normal * force); // apply a force pushing the object } } This will push any rigidbody hit in the opposite direction. If you want to only push down rigidbodies with the character's "weight", check the hit.normal elevation angle (hit.normal is a normalized vector, thus its Y coordinate is numerically equal to the sine of the elevation angle):
... if (hit.rigidbody && hit.normal.y > 0.96){ // if hit.normal > 75 degrees... hit.rigidbody.AddForce(Vector3.down * force); // apply a down force }
Answer by liszto · Feb 21, 2012 at 02:35 PM
Thanks for this answer. This work but my teammate wants more x_x. Because he wants know when the player hit and when he stops hitting.
But yeah, addForce() function works great to simulate that kind of interaction :)