- Edited
Hello
I'm trying to get an object to move based on its rotation. I have the left and right keys turning the object in their respective direction. However, I can't figure out how to move the object forward or backward based on the direction. the code used to rotate the object is:
extends KinematicBody2D
var speed = 100
var trav = 50
var vel = Vector2()
func _ready():
set_process(true)
func _process(delta):
#anticlockwise rotation
if Input.is_key_pressed(KEY_RIGHT):
set_rotd(get_rotd() - trav * delta)
#clockwise rotation
elif Input.is_key_pressed(KEY_LEFT):
set_rotd(get_rotd() + trav * delta)
#forward movement
elif Input.is_key_pressed(KEY_UP):
vel = Vector2(speed , delta )
#backward movement
elif Input.is_key_pressed(KEY_DOWN):
vel = Vector2(-speed , delta)
else:
vel = Vector2(0, 0)
if Input.is_key_pressed(KEY_X):
print(get_rotd())
set_pos(get_pos() + vel * delta)
essentially, when KEY_UP is pressed, I want it to move forward relative to the rotation it is, and when KEY_DOWN is pressed it moves backward. How could I accomplish this?