How to make my player move in facing direction

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Aego

How do you make my player move in it’s facing direction?
For example, My player would rotate to the right and once I press a certain key, It should move forward or backward on where it’s facing instead of moving up or down the y axis.

Help would be appreciated. Thank you.

:bust_in_silhouette: Reply From: kidscancode

You can use the transform for this. I’m assuming you’re in 2D, but the principle is the same in 3D.

In 2D transform.x is the object’s local forward axis. So to always move forward regardless of rotation, you could do this for example:

func _process(delta):
    if Input.is_action_pressed("forward"):
        position += transform.x * speed * delta