How do I cap the max speed of my vehicle?

Hello everyone. Hopefully someone can help me with this issue. I am using a basic vehicle script by Andrew Gotow for a vehicle I’m testing out in Unity. I need to find a way to cap off the vehicle’s maximum speed. Within Andrew’s script he does have a line commented as such:

// This is to limith the maximum speed of the car, adjusting the drag probably isn’t the best way of doing it,
// but it’s easy, and it doesn’t interfere with the physics processing.
rigidbody.drag = rigidbody.velocity.magnitude / 150;

However this doesn’t seem to have any impact on the speed of the car in my scene…my question is: Is there a better, more efficient way of capping the vehicle’s max speed…maybe an exposed variable in the editor I can set?

Gotows car tutorial uses gear ratios, torque and rpm, trying changing those VAR’s to get the vehicle out of lightspeed, or the doldrums. hope this helps!

Hey JamesArndt, did you find out how to cap the max speed?
I am also finding it difficult to do so…

http://vonlehecreative.wordpress.com/2010/02/02/unity-resource-velocitylimiter/

That’s where I ended up finding my answer and his solution works beautifully.

this was my fix, speed was the magnitude of the rigid body, this made the car speed up and get capped at what ever you set maxSpeed as.

float maxSpeed = 0; //what ever you want

privatevoidCapSpeed(){
if(speed>maxSpeed){
speed=0f;
if(rb.velocity.magnitude>maxSpeed){
rb.velocity=rb.velocity.normalized*maxSpeed;
}
}
}

1 Like