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

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.