Creating script for player controlled character

I am new to Unity and c# and have came from vb.net console programs.

I am trying to create a game similar to asteroids in terms of player movement in unity using the 2d mode, from a top down perspective. My player controlled character has a 2d rigidbody and i would like to use the “w” and “s” keys for forward/reverse thrusters.

This is the code i have created for forwards thrusters:

void FixedUpdate () {
		if (Input.GetKey () = ("up")) //test user input
						this.gameObject.rigidbody2D.AddForce (Vector2.up * 10f); //Increase player velocity

	}

Any help on how to make it work would be much appreciated.

Thanks, aljowen

The easiest way to solve these problems is to pay close attention to the sample script in the scripting reference. You are not using GetKey() correctly. Try this:

void FixedUpdate () {
       if (Input.GetKey(KeyCode.UpArrow)) //test user input
                 this.gameObject.rigidbody2D.AddForce (transform.up * 10f); //Increase player velocity
 
}

Note I change Vector3.up to transform.up. Vector3.up is the world up. ‘transform.up’ is the local up of the object. This will allow your ship to travel in the direction you point it.