How can i limit the max speed of an car and boost the acceleration

First of all sorry for my bad English.
I am making a car but it accelerates as a train but the top speed is infinite.
Does anyone know how I can give it limits and boost the acceleration? I tried playing with motor force and adding/reducing mass.
This is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarController : MonoBehaviour
{
    private float m_horizontalInput;
    private float m_verticalInput;
    private float m_steeringAngle;

    public WheelCollider FrontD;
    public WheelCollider RearD;
    public WheelCollider FrontP;
    public WheelCollider RearP;
    public Transform FrontDT;
    public Transform RearDT;
    public Transform FrontPT;
    public Transform RearPT;
    public float maxSteerAngle = 30;
    public float motorForce = 50;

    public void GetInput(){
      m_horizontalInput = Input.GetAxis("Horizontal");
      m_verticalInput = Input.GetAxis("Vertical");
    }

    private void Steer(){
      m_steeringAngle = maxSteerAngle * m_horizontalInput;
      FrontD.steerAngle = m_steeringAngle;
      FrontP.steerAngle = m_steeringAngle;
    }

    private void Accelerate(){
      RearD.motorTorque = m_verticalInput * motorForce;
      RearP.motorTorque = m_verticalInput * motorForce;
    }

    private void UpdateWheelPoses(){
      UpdateWheelPose(FrontD, FrontDT);
      UpdateWheelPose(RearD, RearDT);
      UpdateWheelPose(FrontP, FrontPT);
      UpdateWheelPose(RearP, RearPT);
    }

    private void UpdateWheelPose(WheelCollider _collider, Transform _transform)
      {
        Vector3 _pos = _transform.position;
        Quaternion _quat = _transform.rotation;

        _collider.GetWorldPose(out _pos, out _quat);

        _transform.position = _pos;
        _transform.rotation = _quat;
     }

    private void FixedUpdate(){
      GetInput();
      Steer();
      Accelerate();
      UpdateWheelPoses();
    }
}

This is what the inspector looks like

Lots of ways to try, each with its own “feel” to the top speed.

  1. You can get the speed from the magnitude of the Rigidbody associated with the car. As that reaches a limit, you can begin to attenuate the torque applied down towards zero.

  2. Another way is to explicitly apply a drag force that scales up as a function of speed.

  3. You can just increase the drag setting on the Rigidbody.

There are probably others.

And how can i increse the acceleration?

What does changing MotorForce do? Are you spinning your wheels? If you want greater than 1 friction coefficient, you might need to add an extra force to the car to fake it.

This is whats happening the car accelerates bad. And breaks bad. as well as if it colides with another object it still drives forward.
https://www.youtube.com/watch?v=OPBjADVKTMA

And yes audio isnt correct yet :smile: btw I started to break at 0:40

You have to find out what is happening: are the wheels skidding? This is likely.

Also, actual wheel collider physics is EXTREMELY difficult to get to work with 4 wheels and a car that moves with any significant speed. This is because the physics is discretely simulated and is constrained as far as the practical dynamic range of values it can deal with (ratio between the largest and smallest forces) given that it uses 32-bit floating point numbers.

It’s generally better to implement your own skate-over-ground physics with whatever cornering logic is involved, otherwise it will be very difficult to get just the right feeling for your car.

Check out some different tutorials for high-speed racing games and you can get an idea for what is involved.

Thanks