Setting the max velocity of a rigidbody

I’ve tried different techniques to make this work but I haven’t figured it out so hopefully someone could help me tailor it to my code, thanks! I’m trying to make it so that my character can only run at a certain speed.

{
    public Transform tr;
    public Rigidbody rb;
    public float maxspeed = 500f;
    public float speed = 450f;
    public float jumpspeed = 600f;
    public GameObject player;
    public bool grounded;
    public GameObject Ground;
    public Animation disappear;
    

    public void Start()
    {
      
        grounded = true;
    }
    public void OnCollisionEnter(Collision theCollision)
    {
        if (theCollision.gameObject.name == "Ground")
        {

            grounded = true;
        }
    }
    public void OnCollisionExit(Collision theCollision)
    {
        if (theCollision.gameObject.name == "Ground")
        {
            
            grounded = false;
        }
    }

    private void FixedUpdate()
    {
        if (rb.velocity.magnitude > maxspeed)
        {
            rb.velocity = rb.velocity.normalized * maxspeed;
        }
        
            if (Input.GetKeyDown("e"))
        {   disappear["disappear"].speed = 1;
            disappear.Play("disappear");
        }
        if (Input.GetKeyUp("e"))
        {
          
            disappear["disappear"].speed = -1;
            disappear["disappear"].time = disappear["disappear"].length;
            disappear.Play("disappear");
        }
        if (Input.GetKey("w") && grounded == true)
        {
            
            rb.AddForce(0, jumpspeed * Time.deltaTime,0);
        }
        if (Input.GetKey("a") && grounded == true)
        {
            rb.AddForce(-speed * Time.deltaTime,0,0);
        }
        if (Input.GetKey("d") && grounded == true)
        {
            rb.AddForce(speed * Time.deltaTime,0,0);
        }
    }
}

Trying to set the velocity of a RigidBody and add force doesn’t always play nice together. As described by this stackexchange post, you probably want to avoid capping the speed by setting velocity – instead, simply stop adding force when the speed limit is reached. In your game, that would look something like the code below:

{
    public Transform tr;
    public Rigidbody rb;
    public float maxspeed = 500f;
    public float speed = 450f;
    public float jumpspeed = 600f;
    public GameObject player;
    public bool grounded;
    public GameObject Ground;
    public Animation disappear;
    
    public void Start()
    {
        grounded = true;
    }

    public void OnCollisionEnter(Collision theCollision)
    {
        if (theCollision.gameObject.name == "Ground")
        {
            grounded = true;
        }
    }

    public void OnCollisionExit(Collision theCollision)
    {
        if (theCollision.gameObject.name == "Ground")
        {
            grounded = false;
        }
    }

    private void FixedUpdate()
    {
        if (Input.GetKeyDown("e"))
        {   
            disappear["disappear"].speed = 1;
            disappear.Play("disappear");
        }
        if (Input.GetKeyUp("e"))
        {
            disappear["disappear"].speed = -1;
            disappear["disappear"].time = disappear["disappear"].length;
            disappear.Play("disappear");
        }

        // only allow adding additional force when the speed is less than the maximum
        if (rb.velocity.magnitude < maxspeed)
        {
            if (Input.GetKey("w") && grounded == true)
            {
                rb.AddForce(0, jumpspeed * Time.deltaTime,0);
            }
            if (Input.GetKey("a") && grounded == true)
            {
                rb.AddForce(-speed * Time.deltaTime,0,0);
            }
            if (Input.GetKey("d") && grounded == true)
            {
                rb.AddForce(speed * Time.deltaTime,0,0);
            }
        }
    }
}

@Alanisaac Thank you very much for your help i’ll test the code later today!