Car goes way beyond the speed limit even though the script already assigned the current speed just to reach the speed limit
Will somebody help me in my problem? I already prepared the car with Colliders, the Rigidbody, and I followed a tutorial (Link: https://youtu.be/Z4HA8zJhGEk - "Simple Car Controller in Unity Tutorial") for scripting the car. I also got the speed of the car in the script, determined the maximum speed of the car by searching it in Google, and added a few modifications to the script. Here now lies the problem: the car goes way beyond the maximum speed although the maximum speed is set and I already putted the codes that I found from https://answers.unity.com/questions/1040591/how-to-set-speed-limit-of-car.html and https://answers.unity.com/questions/1067097/car-speed-limit-unity5.html onto the script. What could I have done wrong in my script so that this will happen? Here is the code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CarController2 : MonoBehaviour { private const string HORIZONTAL = "Horizontal"; private const string VERTICAL = "Vertical";
public Rigidbody m_Rigidbody;
public float torque;
private Vector3 m_EulerAngleVelocity;
private float horizontalInput;
private float verticalInput;
private float currentsteerAngle;
private float currentbreakForce;
private float timeCount;
private bool isBreaking;
[SerializeField] private float motorPower;
[SerializeField] private float breakForce;
[SerializeField] private float accelerationRate;
[SerializeField] private float currentSpeed;
[SerializeField] private float maxSpeed = 195;
[SerializeField] private float maxSteerAngle;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform frontRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
private void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
CarTurn();
SpeedOfCar();
}
private void GetInput()
{
horizontalInput = Input.GetAxis(HORIZONTAL);
verticalInput = Input.GetAxis(VERTICAL);
isBreaking = Input.GetKey(KeyCode.Space);
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("You hitted the brakes.");
}
}
private void HandleMotor()
{
rearLeftWheelCollider.motorTorque = verticalInput * motorPower;
rearRightWheelCollider.motorTorque = verticalInput * motorPower;
currentbreakForce = isBreaking ? breakForce : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
frontLeftWheelCollider.brakeTorque = currentbreakForce;
frontRightWheelCollider.brakeTorque = currentbreakForce;
rearLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
currentsteerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = currentsteerAngle;
frontRightWheelCollider.steerAngle = currentsteerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
private void CarTurn()
{
float turn = Input.GetAxis("Horizontal");
m_Rigidbody.AddTorque(transform.up * torque * turn);
}
private void SpeedOfCar()
{
currentSpeed = m_Rigidbody.velocity.magnitude*3.6f;
currentSpeed = Mathf.Round(currentSpeed);
accelerationRate = maxSpeed/16.7f;
accelerationRate = Mathf.Round(accelerationRate);
if(currentSpeed <= 195){
for(int i = 0; i < 4; i++) { rearLeftWheelCollider.motorTorque = verticalInput * motorPower; }
for(int i = 0; i < 4; i++) { rearRightWheelCollider.motorTorque = verticalInput * motorPower; }
}
else {
for(int i = 0; i < 4; i++) { rearLeftWheelCollider.motorTorque = 0; }
for(int i = 0; i < 4; i++) { rearRightWheelCollider.motorTorque = 0; }
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613041907im_/https://answers.unity.com/themes/thub/images/avi.jpg)