- Home /
The question is answered, right answer was accepted
Remove Force from Moving Object
I have a rolling ball that the player can move around, which I'm doing using rigidbody.AddForce. I want to include a button (Left Shift, in my current code) that stops the ball completely, but am running into an issue. The ball stops still the moment the button is pressed, but afterward will continue rolling just as it was before. I've tried adjusting the ball's physics material friction and the AddForce Force Mode, neither seem to make any difference.
Can anyone help me to get the ball to halt completely, and not move again until the player's next input?
function FixedUpdate ()
{
if(Input.GetKeyDown(KeyCode.LeftShift))
rigidbody.velocity = Vector3.zero;
if(Input.GetKey("up"))
rigidbody.AddForce(0,0,speed);
if(Input.GetKey("down"))
rigidbody.AddForce(0,0,-speed);
if(Input.GetKey("right"))
rigidbody.AddForce(speed,0,0);
if(Input.GetKey("left"))
rigidbody.AddForce(-speed,0,0);
}
Answer by robertbu · Sep 26, 2013 at 01:21 AM
You also need to set the angularVelocity to zero:
if(Input.GetKeyDown(KeyCode.LeftShift)) {
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
}
I also had this problem and overlooked the angular velocity. Thanks robertbu! You've helped me too!
rigidbody.angularVelocity = Vector3.zero; > this does not work though i used rigidbody.angularVelocity = 0f;
It might be because of update. Since csgeorge said it worked :)
can you help me as well? i want my player to stop the forces when he moves right or left. but if hes in the air that makes him float. i want to only stop his velocity on X axis. so that it stays the same on the Y. i tried putting RigidBody.Velocyty.x but it gives me an error. is there a way?
Follow this Question
Related Questions
Move RigidBody character relative to camera. 2 Answers
Rigidbody.MovePosition doesn't move reliably? 1 Answer
Weird Movement 1 Answer
help fix my script 3 Answers
How to make an object wait until it finishes moving? 0 Answers