- Home /
How do I limit the speed of my car?
I am making a car game and i cannot limit the speed, but it the speed just goes unlimited. Below is the code which is not limiting my speed and converts the speed to kmph.
function Control (){
currentSpeed = 2*22/7*WheelRL.radius*WheelRL.rpm*60/1000; //this converts the speed to kmph
currentSpeed = Mathf.Round(currentSpeed); //this converts the speed to kmph
if (currentSpeed < topSpeed && currentSpeed > -maxReverseSpeed){ //these lines limit my speed
WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
}
else {
WheelRR.motorTorque =0;
WheelRL.motorTorque =0;
}
Below is my whole car control code:-
#pragma strict
var WheelFL : WheelCollider;
var WheelFR : WheelCollider;
var WheelRL : WheelCollider;
var WheelRR : WheelCollider;
var WheelFLTrans : Transform;
var WheelFRTrans : Transform;
var WheelRLTrans : Transform;
var WheelRRTrans : Transform;
var lowestSteerAtSpeed : float = 50;
var lowSpeedSteerAngel : float = 20;
var highSpeedSteerAngel : float = 5;
var decellarationSpeed : float = 30;
var maxTorque : float=50;
var currentSpeed : float;
var topSpeed : float = 150;
var maxReverseSpeed : float = 50;
var backLightObject : GameObject;
var idleLightMaterial : Material;
var brakeLightMaterial : Material;
var reverseLightMaterial : Material;
private var mySidewayFriction : float;
private var myForwardFriction : float;
private var slipSidewayFriction : float;
private var slipForwardFriction : float;
function Start () {
rigidbody.centerOfMass.y = -0.9;
}
function SetValues (){
myForwardFriction = WheelRR.forwardFriction.stiffness;
mySidewayFriction = WheelRR.sidewaysFriction.stiffness;
slipForwardFriction = 0.05;
slipSidewayFriction = 0.085;
}
function FixedUpdate () {
Control();
}
function Control (){
currentSpeed = 2*22/7*WheelRL.radius*WheelRL.rpm*60/1000; //this converts the speed to kmph
currentSpeed = Mathf.Round(currentSpeed); //this converts the speed to kmph
if (currentSpeed < topSpeed && currentSpeed > -maxReverseSpeed){ // this line and the below lines is the code which limits my speed
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");
WheelFR.steerAngle = 20 * Input.GetAxis("Horizontal");
WheelFL.steerAngle = 20 * Input.GetAxis("Horizontal");
var speedFactor = rigidbody.velocity.magnitude/lowestSteerAtSpeed;
var currentSteerAngel = Mathf.Lerp(lowSpeedSteerAngel,highSpeedSteerAngel,speedFactor);
currentSteerAngel *= Input.GetAxis("Horizontal");
WheelFL.steerAngle = currentSteerAngel;
WheelFR.steerAngle = currentSteerAngel;
}
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;
BackLight();
}
function BackLight (){
if (currentSpeed > 0 && Input.GetAxis("Vertical")<0){
backLightObject.renderer.material = brakeLightMaterial;
}
else if (currentSpeed < 0 && Input.GetAxis("Vertical")>0){
backLightObject.renderer.material = brakeLightMaterial;
}
else if (currentSpeed < 0 && Input.GetAxis("Vertical")<0){
backLightObject.renderer.material = reverseLightMaterial;
}
else{
backLightObject.renderer.material = idleLightMaterial;
}
}
You can edit the rigidbody.drag values to limit your speed.
Thanks Evil Tak it worked and i wanted to ask one more thing that my car goes to a little left automatically.
This is a known bug of the sideways slip of wheelcolliders. You can set the sideways slip higher, but that results in a sticky or too grippy wheel. This usually occurs while driving on a flat plane, but on rough terrain you can't notice it much. Or you may use another wheel alternative, like UnityCar's wheel, by using physics raycasts. Personally, I feel that the built in wheel collider's are too unrealistic. $$anonymous$$aybe the unity 5 update with PhysX 3.3 will be different.