Setting max speed of spacecraft

Hello, I’m working on a space game and i want to set the max speed of my ship so it doesn’t increase speed overtime, and i also like some little help on improving this.
Script:

	private float maxSpeed = 100;
	public float shipRotationSpeed;
	public float shipSpeed;
	private Rigidbody rb;
	private float mouseSensativity = 5;

	// Use this for initialization
	void Start () {
	
		rb = GetComponent<Rigidbody> ();

	}
	
	// Update is called once per frame
	void FixedUpdate () {

		float moveHorizontal =- Input.GetAxis ("Horizontal") * shipRotationSpeed;
		float moveVertical = Input.GetAxis ("Vertical") * shipSpeed;

		Vector3 movement = new Vector3 (moveVertical,0.0f ,0 );

		float upDownRotation = Input.GetAxis ("Mouse Y") * mouseSensativity;

		transform.Rotate (moveHorizontal, -upDownRotation, 0);

		movement = transform.rotation * movement;

		rb.AddForce (movement * shipSpeed);

	}
}

Thanks for your support!

no problem :)

1 Answer

1

You can achieve that by clamping the velocity of the rigidbody:

float curSpeed = rb.velocity.magnitude;
if(curSpeed>this.maxSpeed) { 
	rb.velocity *= (this.maxSpeed/curSpeed);
}