Trying to make a game object move in the direction it is facing.

First off, I am completely new to this, so please be patient with me :slight_smile: So I am making a game (trying to) in which holding the space bar powers up your vehicle and releasing it propels it forward for a certain distance depending on how long you held the button down. I have the ship rotating when I press the arrow keys, but I cant seem to get the ship to move in the direction it is facing.I tried to get the horizontal and vertical axis using this code

float thrust = 1.0f;

	//Retrieve axis information
	float inputX = Input.GetAxis("Horizontal")* thrust * Time.deltaTime;
	float inputY = Input.GetAxis ("Vertical")* thrust * Time.deltaTime;

	print (inputY);
	print (inputX);

but when I print to the console those values return as the following

45562-capture.png

Not sure why. Don’t really know how much more info you need to know what I mean, so just let me know if I need to be more specific. Thanks in advance.

It is actually a lot easier than that.
You can use transform.forward (or rigidbody.forward, in your case) to grab the forward facing direction of the current game object.
When using “forward”, be sure to set the Space to World or else you will see some weird behavior.

Below is a code sample I wrote for you to get you started. My function uses WASD rather than the space bar, but I think you will get the idea.
In my code sample, I use the transform rather than the rigid body, but they should be interchangeable for your purposes.

You will need to place this code inside a class within your script.

	float acceleration = 0.0f;
	// Use this for initialization
	void Start () {
	}

	// Update is called once per frame
	void Update() {
		MoveCharacter();
		RotateCharacter();
	}

    //Gathers input to move the character
	void MoveCharacter () {
		//If the player presses W
		if (Input.GetKey (KeyCode.W))
		{
		//Increase forward acceleration
			if (acceleration < 5.0f)
			{
				acceleration += 0.1f;
			}
		}
		//If the player presses S
		if (Input.GetKey (KeyCode.S))
		{
			//Accelerate in the reverse direction.
			if (acceleration < 5.0f)
			{
				acceleration -= 0.1f;
			}
		}
		//If the player is not pressing forward or backward.
		if (!Input.GetKey (KeyCode.W) && !Input.GetKey (KeyCode.S))
		{
			//Decrease acceleration.
			if (acceleration > 0.0f)
			{
				acceleration -= 0.1f;
			}
		}
		//Compensate for floating point imprecision.
		//If the player is not supposed to be moving, explicitly tell him so.
		if (acceleration > -0.05f && acceleration < 0.05f)
		{
			acceleration = 0.0f;
		}
		//Move the character in its own forward direction while taking acceleration and time into account.
		//The Time.deltaTime maintains consistent speed across all machines by syncing the speed with time.
        //Here is where the magic happens.
		transform.Translate(transform.forward*acceleration*Time.deltaTime, Space.World);
	}

    //Gathers input to rotate the character.
	void RotateCharacter()
	{
		//If the player presses D
		if (Input.GetKey (KeyCode.D))
		{
			//Rotate the current game object, using deltaTime for consistency across machines.
			transform.Rotate (transform.up, 100.0f*Time.deltaTime, Space.World);
		}
		//If the player presses A
		if (Input.GetKey(KeyCode.A))
		{
			//Rotate the current game object's transform, using deltaTim for consistency across machines.
			transform.Rotate (transform.up, -100.0f*Time.deltaTime, Space.World);
		}
	}

Use transform.forward to get dirrection.

transform.position += -transform.forward * Time.deltaTime;