Question by
$$anonymous$$ · Jun 22, 2016 at 03:59 PM ·
scritping
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!
Comment
Answer by Mindmapfreak · Jun 23, 2016 at 07:16 PM
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);
}
Your answer

Follow this Question
Related Questions
Quick Question on how to write a player class 1 Answer
ParticleSystem.Emit Deprecated? 1 Answer
First Person and Lifting 1 Answer
Add items from array to another array by parameter value 0 Answers
hide object with mouse enter 0 Answers