- Home /
Pushing rigidbody with FPS Controller
I am making an FPS using the FPS controller that comes with Unity. I have added this script:
// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * pushPower;
}
It pushes the objects, but either its shaky, or it flips the object over. I just want a smooth push (like Source) . PLEASE HELP!
Answer by Dorque · Feb 11, 2013 at 10:36 PM
You just need to go to your rigidbody and Freeze Rotation for X, Y, and Z.
Answer by cdrandin · Jan 10, 2013 at 09:00 PM
For a FPS, may I recommend using a character controller. Rigidbody is for enabling Unity's true physics engine and will interact truthfully how a real life object might act. Unity's boot camp tutorial teaches A LOT of what you want to do for a FPS. I suggest following the tutorial for it, if any, if not then just dissect the code.
I am using a character controller. The FPS character controller that comes with Unity. I want to be a able to push rigid bodies around (using the character controller) The script above is another script to make objects push-able by the FPS controller.
Answer by MojopinStudios · Feb 06, 2013 at 05:10 PM
Have you tried using RigidBody.AddForce
instead of changing the velocity of the RigidBody
?
So try:
body.AddForce(pushDir * pushPower);
Haven't tested as I'm not in front of my dev PC, but if you pass in the correct Vector3
to AddForce
it should work.