- Home /
Rigidbody Momentum
Hello Everyone,
I have a vehicle that's a rigidbody. The issue is that when I release the movement key, it still has too much momentum and flies off the map.
My movement code:
if (grounded){
rigidbody.AddRelativeForce(Vector3(0,0,movement *400000)); //move forward/backward
rigidbody.AddRelativeTorque (Vector3(0,rotate* 20000,0 )); //turn left/right
}
So how do I make the vehicle slow down when not pressing the movement key? My mass is 5000; otherwise the vehicle falls too slowly.
I appreciate your help- Hyperion
Answer by aldonaletto · Aug 03, 2013 at 11:35 PM
You can increase the Drag field (rigidbody.drag property) - this will reduce the momentum faster. Another possibility is to implement your own friction, which may become active only when you want. Since all the momentum is stored in rigidbody.velocity and rigidbody.angularVelocity, simply multiplying them by a number slightly below 1 each FixedUpdate kills the momentum - like this:
var brake = false; // set it to true to kill the momentum
var friction: float = 0.95; // 1 means no friction, 0 stops instantly
function FixedUpdate(){
if (break){
rigidbody.velocity *= friction;
rigidbody.angularVelocity *= friction;
}
}
NOTE: There's a problem with this approach: it works even when the vehicle is falling, making it fall slowly like in a tar pit. If the world vertical direction is Y, a possible way to work around this is to multiply only X and Z, like this:
function FixedUpdate(){
if (brake){
var vel = rigidbody.velocity;
vel.x *= friction; // modify only x and z components
vel.z *= friction;
rigidbody.velocity = vel;
// angularVelocity should be reduced as a whole:
rigidbody.angularVelocity *= friction;
}
}
EDITED: Oops! I wrote break, which is a reserved word, and it should be brake - answer fixed now.
Hi, thanks for the answer. One issue: it says "break" is an unexpected token, so may I replace break with something else?
Ok, I replaced break and set it to true and it works! Thank you very much!
$$anonymous$$y bad! It should be brake ins$$anonymous$$d of break, which is a reserved word. Answer fixed.
Your answer
Follow this Question
Related Questions
create momentum on a player that should always moves forward 2 Answers
[Physics] Compensating for Moving/Rotating Terrain 0 Answers
Is it possible to increase stopping distance with forcemode.velocitychange? 0 Answers
How to Gain Forward Speed and Momentum with a Rigidbody? 1 Answer
Rigidbody momentum? 1 Answer