# Help with programming character movement for a ship

Attention Topic was automatically imported from the old Question2Answer platform.

Hello, I am new to using Godot and after doing a tutorial on 2d character movement I wanted to try and write a script which would allow you to move a ship using the four arrow keys, however it would only ever move forward relative to the ship’s sprite (ships don’t strafe). The idea is that when you wanted to move the ship in a different direction it would have to start moving forward and turn at a certain speed until it matches the direction of the input vector (arrow keys), as shown here:

I wasn’t exactly sure how to accomplish this, and after some trial and error I sort of approximated the effect by simply having the ship turn in the direction of the input vector while moving forward at a base speed. Here’s the code:

``````extends CharacterBody2D

const ACCELERATION = 100
const MAX_SPEED = 100
const FRICTION = 50
const ROTATION_SPEED = 0.5

func _physics_process(delta):
var input_vector = Vector2.ZERO

input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()

if input_vector != Vector2.ZERO:
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
if rotation != input_vector.angle():
rotation = velocity.move_toward(input_vector, ROTATION_SPEED * delta).angle() + PI / 2
else:
velocity = velocity.move_toward(transform.y * -10, FRICTION * delta)

move_and_slide()
``````

This sort of works, however it isn’t entirely accurate since the ship doesn’t actually move forward exclusively, and at slow speeds it can just flip in another direction which isn’t what I want. I’m sure there is a way to do this much more easily with vector math or move_toward(), I just can’t figure it out.