Why does rigidbody2D.AddForce not move the object in the direction of the transform?

I am trying to create a basic 2D spaceship object as part of learning unity. The object should be able to move forward in the direction it is facing and rotate using the WASD keys.

My script currently reads as follows:

public float thrust = 1.0f;

void Update () {

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

	//print (inputY);

	rigidbody2D.AddTorque(-inputX);
	rigidbody2D.AddForce(transform.forward * inputY);

}

Rotation works correctly, however i can see no change either on screen or in the inspector for Translation. As you can see I have checked that the input from the keys is returning a value via printing to console and it is working correctly.

Thanks in advance.

There’s no forward in 2D) Use transform.right for x coordinate and transform.up for y

Hi,

I am not sure but I believe the transform.forward is effectively a Vector3.forward which has 1 set on the z axis. This will be ignored by 2D physics stuff. Try changing it to transform.right.

Hi!

void Update () {
    
            if (Input.GetKey(KeyCode.W)) {
        
                rigidbody2D.AddForce(transform.up);
        
            }
        
            if (Input.GetKey(KeyCode.S))
            {
                rigidbody2D.AddForce(-transform.up);
            }
        
            if (Input.GetKey(KeyCode.A))
            {
                rigidbody2D.AddTorque(3f);
            }
        
            if (Input.GetKey(KeyCode.D))
            {
                rigidbody2D.AddTorque(-3f);
            }
}