I am trying to get this ship sprite to be able to rotate with A and D and also strafe left and right with the mouse based on where the mouse is. But if I move the ship away from the center of the camera it doesn't work properly as shown below:

Will work fine if I do not have the sprite following the mouse position:

But I would like the player ship to follow the mouse on the x axis for strafing around. I am guessing I need to have to mouse position rotate with how the camera is as well? But I am not sure.

Here is the code for the player:

extends KinematicBody2D

export var move_speed = 50

var rotation_dir = 0
var move_dir = Vector2(0,0)
var velocity = Vector2()

export (int) var speed = 200
export (float) var rotation_speed = 1.5

func _physics_process(delta):
	get_input()
	rotation += rotation_dir * rotation_speed * delta
	velocity = move_and_slide(velocity)
	$ship.global_position.x = get_global_mouse_position().rotated(rotation).x

func _toggle_shield():
	$shield.visible = !$shield.visible

func get_input():
	rotation_dir = 0
	velocity = Vector2()
	if Input.is_action_pressed("right"):
		rotation_dir += 1
	if Input.is_action_pressed("left"):
		rotation_dir -= 1
	if Input.is_action_pressed("down"):
		velocity = Vector2(0, speed).rotated(rotation)
	if Input.is_action_pressed("up"):
		velocity = Vector2(0, -speed).rotated(rotation)

Any help would be great.

4 days later

I was able to figure it out, I discovered there is a get_local_mouse_position and was able to get the correct behavior now.

a year later