# Speed limit

How do I simply set a top speed so the car won’t go over that speed. Could you please check my script for me, thanks.

``````var wheelRR : WheelCollider;
var wheelRL : WheelCollider;
var wheelFR : WheelCollider;
var wheelFL : WheelCollider;
var lowestSteerAtSpeed : float = 50;
var lowSpeedSteerAngel : float = 10;
var highSpeedSteerAngel : float = 1;
var maxTorque : float = 50;
var decellarationSpeed : float = 30;
var currentSpeed : float;
var topSpeed : float = 150;
var wheelFLTrans : Transform;
var wheelFRTrans : Transform;
var wheelRLTrans : Transform;
var wheelRRTrans : Transform;

function Start () {
rigidbody.centerOfMass.y = -2.1;
}

function FixedUpdate () {
Controle ();

}
function Update(){
wheelFLTrans.Rotate(wheelFL.rpm/60*360*Time.deltaTime,0,0);
wheelFRTrans.Rotate(wheelFR.rpm/60*360*Time.deltaTime,0,0);
wheelRLTrans.Rotate(wheelRL.rpm/60*360*Time.deltaTime,0,0);
wheelRRTrans.Rotate(wheelRR.rpm/60*360*Time.deltaTime,0,0);
wheelFLTrans.localEulerAngles.y = wheelFL.steerAngle - wheelFLTrans.localEulerAngles.z;
wheelFRTrans.localEulerAngles.y = wheelFR.steerAngle - wheelFRTrans.localEulerAngles.z;

}

function Controle () {
currentSpeed = Mathf.Round(currentSpeed);
if(currentSpeed > topSpeed) {
wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
}
else {
wheelRR.motorTorque =0;
wheelRL.motorTorque =0;
}
if (Input.GetButton("Vertical")==false) {
wheelRR.brakeTorque = decellarationSpeed;
wheelRL.brakeTorque = decellarationSpeed;
}
else{
wheelRR.brakeTorque = 0;
wheelRL.brakeTorque = 0;
}

wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
var speedFactor = rigidbody.velocity.magnitude/lowestSteerAtSpeed;
var currentSteerAngel = Mathf.Lerp(lowSpeedSteerAngel,highSpeedSteerAngel,speedFactor);
currentSteerAngel *= Input.GetAxis("Horizontal");
wheelFR.steerAngle = currentSteerAngel;
wheelFL.steerAngle = currentSteerAngel;

}
``````

You may get help from this simple script. Add it to your code.

``````public var Speed : float;
public var maxSpeed : float = 150;

Speed = rigidbody.velocity.magnitude * 3.6f;

//Speed Limiter.
if(Speed > maxSpeed){
Wheel_FL.motorTorque = 0;
Wheel_FR.motorTorque = 0;
}else{
Wheel_FL.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
Wheel_FR.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
}
``````