# How to set rigidbody velocity and angularVelocity to Vector3.zero over time?

Hey!

I’m currently working on a vehicle system for my game, and when you accelerate, the vehicle’s rigidbody adds a thrust force, but when you hit ‘S’ to brake, it stops inmediatly (I’m setting velocity and angularVelocity to zero)

Here’s the code:

``````if (carReverse == false) {
if (Input.GetKey (KeyCode.W) && carCurrentSpeed <= carMaxSpeed) {
} else if (Input.GetKey (KeyCode.W) == false && Input.GetKey (KeyCode.S) == false && carCurrentSpeed >= 0.03f) {
} else if (Input.GetKey (KeyCode.W) == false && Input.GetKey (KeyCode.S) && carCurrentSpeed >= 1f) {
vehRb.velocity = Vector3.zero * Time.deltaTime;
vehRb.angularVelocity = Vector3.zero * Time.deltaTime;
}
}
``````

I tried putting ‘Time.deltaTime’ to see if it would stop as time passed but it instantly stops the vehicle, and by the way this isn’t the classic W to drive forward and S to drive backwards, this thing I’m trying to do is to hit ‘W’ to increase speed either forward or backwards depending if ‘carReverse’ is true or false, and the S key is only to stop.

You could multiply the velocity and angularVelocity with a float between 0 and 1. By doing this, the vectors would slowly become smaller and smaller, until they get to a critical point, where you should set them to zero.

Note that if you want to use this method with Time.deltaTime to make it frame rate independent, the new float should be bigger than 1 to compensate Time.deltaTime, and the product should be around 0.95 - 1, or something like that.

``````vehRb.velocity *= Time.deltaTime * slowingScale;
vehRb.angularVelocity *= Time.deltaTime * slowingScale;

if(vehRb.velocity.magnitude < criticalValue) vehRb.velocity = Vector3.zero;
if(vehRb.angularVelocity.magnitude < criticalValue) vehRb.angularVelocity = Vector3.zero;
``````

This solution is easy to implement, however it is not realistic - it is for calculating friction and other things. You will slow down too fast at the beginning, and too slow at the end. You may want to use a velocity-independent mode of braking.

For example, you could try to decrease the length of the vector by a fixed value. You can achieve this by getting the current magnitude, normalizing the vector, then applying the decreased magnitude again.

``````vehRb.velocity = vehRb.velocity.normalized * (vehRb.velocity.magnitude - slowingScale);

if(vehRb.velocity.magnitude < criticalValue) vehRb.velocity = Vector3.zero;
``````

The only thing that changes here is that the slowingScale has to be figured out by experimenting.