Space Ship Speed Controls

Hey!
I’m relativley new to Unity, so if this seems like a basic question, I’m sorry, but I’ve spent the last hour trying to figure it out.
How would I make a variable ‘speed’ go up and down, as I hit the plus and minus keys. I want it to be relatively slow, so the player has a degree of control over it.
Here’s my code, as small as it is.

var speed = 0.0;
function Update () 
{
	if(Input.GetKeyDown(KeyCode.Plus)){
		speed = speed +1;
		Debug.Log(speed);
	}
}

Also where could I find a reference of the key names.
Thanks
Peter

my recommendation is to do it the following way.

var speed = 0.0; 
function Update () 
{ 
   if(Input.GetKeyDown("speed-up")){ 
      speed = speed +1; 
      Debug.Log(speed); 
   } 
}

you will also need to add an input “speed up” to your inputs list. the key for the keypad’s + sign is [+] (with the [ ]'s) in the input manager. This will also allow you to change the key from the input box instead of from the script itself.
with the Input.GetKeyDown option function you can simply use the name assigned to the input you want.

thanks, but how do i change the key input names,
all i can seem to do is edit axis’

Try this first using the up/down/w/s keys :

var speed = 1;

function Update() 
{

        if (Input.GetAxis("Vertical")) {
            translation = Input.GetAxis("Vertical") * speed * Time.deltaTime;
            transform.Translate(0.0, 0.0, translation);
        }

}

Once you know how the above is working create a new input axis (i.e increment the ‘Size’ value) and call it Speed and set the negative and positive buttons and replace ‘Vertical’ with ‘Speed’ in the above code. You may also want to use GetAxisRaw rather then GetAxis so check the docs.

if you click to the right of the “name field” where it shows the name itself, you can edit it there.

Ok, I’ve sort of figured out how I’m going to do this. Right now, I’m not worrying about changing the speed, I’ll work that in later.
How it’s going to work:

  1. The ship will move forward at a constant speed
  2. The player will be able to change the speed withing certain parameters (later)
  3. The player will be able to rotate the ship on two axis (four directions)
  • x axis, and z axis
    You will not be able to pan the ship

I have #1 working so far, but I can’t seem to get the ship to rotate. I know what should work, (or what seems like it should) but it doesn’t seem to be correct, and I can’t find any errors.

Here’s the code:

var shipSpeed = 0;
var shipRotSpeed = shipSpeed/2;
function Update ()
{
	transform.Translate(0, 0, shipSpeed);
	if (Input.GetAxis("Vertical")){
		var shipRotate = Input.GetAxis("Vertical") * shipRotSpeed * Time.deltaTime;
		transform.Rotate(shipRotate, 0, 0);
	}
}

Any help is greatly appreciated, and thank you to those who have already given their input.

Thank you
Peter

var shipSpeed : float = 1.0; // Move 1.0 unit per second
var shipRotSpeed : float = 90.0; // Rotate 90 degrees per second
 
function Update() {

        transform.Translate(0, 0, shipSpeed * Time.deltaTime);

    if (Input.GetButton("Vertical")) {
        
        var direction = Input.GetAxisRaw("Vertical") > 0 ? 1 : -1; 
        var shipRotate = direction * shipRotSpeed * Time.deltaTime;

        transform.Rotate(shipRotate,0,0);

    }
}

Thanks, but could you explain some of that, because you lost me pretty early on.
I’m still a beginner, so use small word :stuck_out_tongue_winking_eye:

Thanks
Peter

Transform.Translate shifts the position of the object by a given amount. On each frame, the time is advanced by an amount equal to Time.deltaTime; since speed * time = distance, this is how far the Translate call will move the object on the current frame.

The calls to the Input class functions are just doing pretty much what you would expect. However, the line:-

var direction = Input.GetAxisRaw("Vertical") > 0 ? 1 : -1;

…is a bit enigmatic. You can create a kind of condensed “if” statement by writing a boolean condition followed by a question mark. The two values after the question mark, separated by a colon are what gets returned if the condition was true or false, respectively. This line is essentially equivalent to:-

if (Input.GetAxisRaw("Vertical") > 0) {
    direction = 1;
} else {
    direction = -1;
}

The direction value is multiplied by the rotation speed to choose the angular direction the object will turn (again scaled by Time.deltaTime). Finally, the call to Transform.Rotate is what actually turns the object.

Ive been taking a break from all this, during my exams, and now I come back, and I realized that it would be so much easier if I got the camera working.

I don’t want to make the camera a child of the ship, because then the ship turns at a point between the camera and the actual ship model, and it’s just looks and feels wrong.

The Camera Follow script is wrong too, so I want to write my own script.

I have tried several ways of doing this already, but I am not that great at programming, (I’m an artist) so here’s what I’ve tried, any help would be AWESOME!!!

  1. I’ve tried creating a separate script for the camera, with a variable that finds the ship game object, and then uses that variable’s x y and z coordinates to translate the camera.

  2. I’ve tried the same thing only inside the ship controls, where the variable finds the camera and translates the camera.

And of course I’ve just thought of something, so I’m going to go and try it, and get back to you, but in case I’m wrong, I’ll post this anyways.

And thank you to everyone who has already helped me, you’ve gotten me farther than I have ever gotten on other game projects. So far, in fact, that a friend is going to start helping me! Relieved Sigh
Thanks,
Peter

Ok, my idea kinda worked, but it won’t do.
What I did was tell the camera to move at the fixed speed of the ship, and then respond directly to the ship controls. The camera moved with the ship, but it didn’t focus on it most of the time.

So now, what I want to know, is:
Is there a way to set the camera up as a variable, and then set its position to another variable equal to the ships position - a number, and then use the camera look script?

eg.

var camera = camera
var shipPos = transform.position(ship)

function Update(){
    camera.transform.translate(shipPos.x, shipPos.y, shipPos.z)
}

Thanks
Peter

If you’ve got the camera’s motion sorted, you can make it look at the target object using transform.LookAt each frame.