Move to dirrection

Godot Version 4.3

I want to create a spaceship so that it accelerate and decelerates using ui_up, ui_down. Also, so that it turns into ui_right, ui_left. It is necessary for the ship to move in the direction of the ship. I don’t understand how to take a direction and have the ship move in that direction.

extends CharacterBody2D
var engine_thrust = 100
var accelerate = Vector2()
var rotation_dir = 0

func get_input():
	if Input.is_action_pressed("ui_up"):
		accelerate += Vector2(engine_thrust, 0)
	if Input.is_action_pressed("ui_down"):
		accelerate -= Vector2(engine_thrust, 0)
	rotation_dir = 0
	if Input.is_action_pressed("ui_right"):
		rotation_dir += 0.1
	if Input.is_action_pressed("ui_left"):
		rotation_dir -= 0.1
	print(rotation_dir)

func _process(delta):
	get_input()
	rotation += rotation_dir
	velocity =  accelerate
	move_and_slide()

How to move in facing direction

Is this what you are wanting to do?

Also, instead of using your own created func get_input() I would use the built-in _input() function. Also, change _process() to _physics_process().

In _input()

If input.is_action_pressed(“up”)
#do stuff to go forward
Elif input.is_action_pressed(“down”)
#do opposite stuff to go backward

if input.is_action_released(“rotate”) #create new input with both left and right rotations keys in it for this one.
#rotation = 0

If input.is_action_pressed(“left”)
#rotate left
Elif input.is_action_pressed(“right”)
#rotate right

Also, instead of having Vector2(engine_thrust, 0) for acceleration. Use the transform that I linked to get accel_dir, then in physics_process velocity += accel_dir * engine_thrust (and if you want it delta equalized) * delta.