How do I set max rigidbody velocity in C#?

This is my code:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

public GameObject player;
public bool down;
public float forward = 15;
public float maxForward = 20;

void Start () {


void Update () {

	if (Input.GetKeyDown(KeyCode.RightArrow))
		down = true;
	if (Input.GetKeyUp(KeyCode.RightArrow))
		down = false;	
	if (down == true)
	if (player.rigidbody.velocity.z > maxForward)
		player.rigidbody.velocity.z = player.rigidbody.velocity.z.normalized;
		player.rigidbody.velocity.z *= maxForward;



It does not work lol. Can someone tell me why & tell me how i can make this work? I want to set a maximum speed of a rigidbody sphere. Thanks! :smiley:

You should use

player.rigidbody.velocity = Vector3.ClampMagnitude(player.rigidbody.velocity, maxForward);

That one line makes sure that the magnitude of the player’s velocity cannot go above a certain value, however it does not limit specific axes.

Also, why don’t you just use


instead of all that messing around with ‘down’? It does the same thing.