How to Set a top speed on a car

I’m using motorTorque attached to the two front wheel colliders to power my car. But the car just seems to get faster and faster. How do I simply set a top speed so the car won’t go over that speed.

Thanks in advance.

I just have a simple bit of code

void Update () {

	FrontLeftWheel.motorTorque = Input.GetAxis("Vertical") * speed;
	FrontRightWheel.motorTorque = Input.GetAxis("Vertical") * speed;
		
	FrontLeftWheel.brakeTorque = 0;
	FrontRightWheel.brakeTorque = 0;
	
	if (Input.GetButton("Jump"))
	{
		FrontLeftWheel.brakeTorque = 20;
		FrontRightWheel.brakeTorque = 20;
	}
	
	FrontLeftWheel.steerAngle = steer * Input.GetAxis("Horizontal");
	
	FrontRightWheel.steerAngle = steer * Input.GetAxis("Horizontal");

}

Not sure why this question was voted negative one as it seems like a legit problem. I would try:

FixedUpdate(){
      //Find the speed by the square root of the velocity of the rigidbody of your car
      speed = rigidbody.velocity.sqrMagnitude;
      
      //Only add motorTorque if your speed is less then max speed
      if(speed < max_speed) {
        FrontLeftWheel.motorTorque = Input.GetAxis("Vertical") * motor_power;
        FrontRightWheel.motorTorque = Input.GetAxis("Vertical") * motor_power;
      }
}

Apply this code to your car…
using UnityEngine;
using System.Collections;

public class TankManager : MonoBehaviour
{
public Transform tank;
public float maxSpeed = 20;
public float tankVelocity = 0;

using UnityEngine;

using System.Collections;

public class CarMovement : MonoBehaviour

{

//car movement

public float maxSpeed = 20;

public float carSpeed = 0;

public float accelaration = 9;

public Update()

{

transform.Translate(new Vector3(0,0,carSpeed * Time.deltaTime));

move();

}

void Move()

{

if(Input.GetKey(KeyCode.W))

{

if(carSpeed <= maxSpeed)

carSpeed += accelaration * Time.deltaTime;

}

}

}

This script worked for me. Hope it works for you! :slight_smile:
You can see in the console that it stays around 100 (the maxspeed i have set).

SCRIPT:

`var WheelFR : WheelCollider;
var WheelFL : WheelCollider;
var WheelRR : WheelCollider;
var WheelRL : WheelCollider;

var Speed = 10;
var MaxSpeed = 100;
var Breaking = 20;
var Turning = 20;

private var CurrentSpeed;

function Update () {
CurrentSpeed = rigidbody.velocity.sqrMagnitude;
Debug.Log(CurrentSpeed + " | " + MaxSpeed);

if (CurrentSpeed < MaxSpeed) {
	WheelRR.motorTorque = Input.GetAxis("Vertical") * Speed;
	WheelRL.motorTorque = Input.GetAxis("Vertical") * Speed;
} else {
	WheelRR.motorTorque = 0;
	WheelRL.motorTorque = 0;
}

WheelRL.brakeTorque = 0;
WheelRR.brakeTorque = 0;

WheelFL.steerAngle = Input.GetAxis("Horizontal") * Turning;
WheelFR.steerAngle = Input.GetAxis("Horizontal") * Turning;

if (Input.GetButton("Jump")) {
	WheelRL.brakeTorque = Breaking;
	WheelRR.brakeTorque = Breaking;
}

}
`

you can try a bigger rigidbody.drag

Read up on the Mathf class, particularly the Clamp method.