Limit speed on local axis

Hello everyone, I’ve been programming a 3d car racing game in N64 style (hopefully I can end up with it on a N64) but I’ve run into a problem, I want to limit the speed on the local Z. Help would be much appreciated. thanks in advance.

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

public class carDrive : MonoBehaviour
{
    public float speed;
    public float agility;
    public Rigidbody rigidbody;
    //public float maxspeed;
    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal") * agility;
       
        rigidbody.AddRelativeForce(Vector3.forward * speed);

        /*if (rigidbody.velocity.x > maxspeed)
        {
            rigidbody.velocity = new Vector3(maxspeed, rigidbody.velocity.y, rigidbody.velocity.z);
        }
       
        if (rigidbody.velocity.y > maxspeed)
        {
            rigidbody.velocity = new Vector3(rigidbody.velocity.x, maxspeed, rigidbody.velocity.z);
        }

        if (rigidbody.velocity.z > maxspeed)
        {
            rigidbody.velocity = new Vector3(rigidbody.velocity.x, rigidbody.velocity.y, maxspeed);
        }*/

        transform.RotateAround(transform.position, transform.up, Time.deltaTime * h);
    }
}

Did you search the web? I found at least one answer there but there are many others.

You can increase the drag setting.

I experimented for myself with the answer given on Discussions and found a way. The script below works both ways, if the speed is below the maximum speed or if it is above that speed.
The commented code at the bottom is a way to achieve speed limit that Unity Technologies doesn’t, apparently, recommend.

using UnityEngine;

public class RigidbodyVelocityLimitTest : MonoBehaviour
{
    [SerializeField] private float speed, maxSpeed;

    private Rigidbody rigidBody;
    private float velocityX, velocityY, brakeForce;

    private void Start()
    {
        // Accessing the rigidbody
        rigidBody = GetComponent<Rigidbody>();
        // Storing velocities
        velocityX = rigidBody.velocity.x;
        velocityY = rigidBody.velocity.y;
    }

    private void FixedUpdate()
    {
        // Moving the vehicle
        VehicleMovement();
    }

    private void VehicleMovement()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            rigidBody.velocity = new Vector3(velocityX, velocityY, speed);

            // Making sure the vehicle speed is limited
            // My way
            // Getting the rigidbody velocity in unit per second
            float magnitude = rigidBody.velocity.magnitude;
            if (magnitude != maxSpeed)
            {
                rigidBody.velocity = new Vector3(velocityX, velocityY, brakeForce);
                float diffSpeed = magnitude - maxSpeed;
                switch (diffSpeed)
                {
                    case > 0:
                        brakeForce = magnitude - diffSpeed;
                        break;
                    case < 0:
                        brakeForce = maxSpeed;
                        break;
                    default:
                        break;
                }
            }
        }
    }

    // Apparently non-recommended way of achieving speed limit
    /*private void SpeedLimit()
    {
        if (rigidBody.velocity.magnitude > maxSpeed)
        {
            rigidBody.velocity = rigidBody.velocity.normalized * maxSpeed;
        }
    }*/
}

But, if you want, you can get to the wanted speed directly:

    private void VehicleMovement()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            rigidBody.velocity = new Vector3(velocityX, velocityY, maxSpeed);
        }
    }

I tried that but found it hard to get the right acceleration - speed ratio. doesn’t give the feel I want.

thanks for the help. the it’s working great now.