space flying system

hey guys i want to make a flight system for spaceships that can be rotated over all axes like in the unity game Phoenix final

and it should be move foreword if i press a button and it should stopp if i press a button

i have scripted something

var rotateSpeed = 3.0;

function Update () 
    var controller : CharacterController = GetComponent(CharacterController);
    //Rotate arond y
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
        //Rotate arond x
    transform.Rotate(0, Input.GetAxis ("Vertical") * rotateSpeed, 0);

@script RequireComponent(CharacterController)

but its only rotating over 1 axe where is the mistake

and one axe is missing and the forward force

thanks for answers

sry for bad english(im german)


thx at all but the c# code wont work but i have still one question

and it should be move foreword if i press a button and it should stopp if i press a button

to add a force

i would be happy if you could answer that

You wouldn't use a character controller for a flying game in any case. Character controllers are only for games like Doom/Quake/Unreal, since among other things they remain upright at all times.

The way I do it is to have a seperate function to handle rotation, which I refactored out from my input script into a more general ship script when I had to handle AI rotation of the ship:

(my code is in C#, but the idea is the same)

public void rotateShip(float xAxis, float yAxis)
    float turnLimit = 0.5f;
    //float minTurnLimit = 0.02f;

    if ((0 != xAxis) || (0 != yAxis))
        //Debug.Log("Moving: " + + " to (" + xAxis + "," + yAxis + ")");

    if (xAxis > turnLimit) { xAxis = turnLimit; }
    if (yAxis > turnLimit) { yAxis = turnLimit; }
    if (xAxis < -turnLimit) { xAxis = -turnLimit; }
    if (yAxis < -turnLimit) { yAxis = -turnLimit; }

    //if (xAxis < minTurnLimit) { xAxis *= 2; }
    //if (yAxis < minTurnLimit) { yAxis *= 2; }
    //if (xAxis > -minTurnLimit) { xAxis *= 2; }
    //if (yAxis > -minTurnLimit) { yAxis *= 2; }

    float rotateX = yAxis * Time.deltaTime * this.turnXRate;
    float rotateY = xAxis * Time.deltaTime * this.turnYRate;
    float rotateZ = 0;

    if (
        (0 != rotateX) ||
        (0 != rotateY) ||
        (0 != rotateZ)
        this.transform.Rotate(rotateX, rotateY, rotateZ);

One important thing to note here is the line:

float rotateX = yAxis * Time.deltaTime * this.turnXRate;

because it ensures a constant rotation rate by multiplying with Time.deltaTime, and it seperates the turn rate out into something that can be easily modified per ship type.

Stylistically, I also just seperate those input calls out into variables right from the get-go:

float inputXAxis = Input.GetAxis("Horizontal");
float inputYAxis = Input.GetAxis("Vertical");

It's only rotating around the y-axis, because both Rotate functions use only values for y. The second rotate should probably use the x value instead.

tranform.Rotate(Input.GetAxis("Vertical") * rotateSpeed, 0, 0);