# Realistic control

Hi, i'm making a space game with some realism but am having difficulty working out the script for the ships. The problem is there is no gravity so the ship is always moving and I need to be able to move the mouse around independently (with the ship in motion) and only when thrust is pressed will the ship change course (in the direction the mouse is then pointing to and altered by inertia) rather than the ship constantly following the mouse position.

Here is my script so far:

``````var rSpeed = 1.0; // **Scale. Speed of the movement**

function FixedUpdate ()
{
MousePosition = Input.mousePosition;
MousePosition.x = (Screen.height/2) - Input.mousePosition.y;
MousePosition.y = -(Screen.width/2) + Input.mousePosition.x;
transform.Rotate(MousePosition * Time.deltaTime * rSpeed, Space.Self);
rigidbody.freezeRotation=true;
}

var flyingSpeed = 0;
var minThrust : float= 0;
var maxThrust : float = 1000;
var Accel = maxThrust / 4;
var Jumpspeed = maxThrust * 5;

var turnSpeed = 10;

function Update () {
//Accelerate the ship useing the thrust button.
if(Input.GetButton("Thrust")){
flyingSpeed = Accel + Mathf.Clamp(flyingSpeed, minThrust, maxThrust - Accel);
}
//decelerates the ship till stop.
if (Input.GetButton("Decellerate")){
flyingSpeed = -Accel + Mathf.Clamp(flyingSpeed, minThrust +Accel, maxThrust );}

if(("Thrust")){
rigidbody.velocity = transform.forward * Time.deltaTime * flyingSpeed; //Apply velocity in Update()

``````

} }

Thanks for any help with this.

If you want realism you could just use something like:

``````if (Input.GetButton("Thrust"))
{
}
if (Input.GetButton("Decellerate"))
{
}

``````

There is no friction in space, so in effect there is no maximum speed. Technically there is a limit as you approach the speed of light, in which case you will have to take into account the distortion of light including blue/red shift to have realism.

I have played around with this stuff in the past and this makes for a very unplayable game. It's is much easier if you make it more like an aeroplane where turning will change the direction of motion.

FYI "thrust" is a measure of force not speed. F = ma.

Petroz's answer seems to do what you are asking about. If you want to cap the max speed as well, just use:

``````if (rigidbody.velocity.sqrMagnitude > maxSpeed * maxSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;

``````