- Home /
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");
}
Answer by SubatomicHero · Apr 02, 2013 at 03:49 PM
Read up on the Mathf class, particularly the Clamp method.
Answer by LyanApps · Apr 02, 2013 at 12:49 PM
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;
}
}
The Car.js in the tutorial at: http://u3d.as/content/unity-technologies/car-tutorial/1qU Computes a (drag) force that is inverse to the velocity that grows as the speed of the car approaches top_speed, and then this force is added to the rigid body to limit it's motion.
Answer by Ruben_Chris · Dec 08, 2013 at 12:28 PM
This script worked for me. Hope it works for you! :) 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;
}
}
Answer by kannan21 · Feb 08, 2013 at 02:37 PM
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;
Answer by kannan21 · Feb 08, 2013 at 02:37 PM
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;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613092340im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 5 wheel collider throttle "stuck". 1 Answer
Applying Torque to wheelcollider 1 Answer
How to stop a car from veering in a particular direction? 0 Answers
The car flies away when wheel collider touch walls or other objects 1 Answer
Add Force or Motor Torque .. Which is better for creating a car? 1 Answer