Find direction of inputs while using a rotating camera to choose appropriate sprite animation

Godot Version

4.3

Question

I am trying to animate a 2d sprite (billboarded) in a 3d world. I have a camera system set up so it can rotate around the sprite when I press q or e. Movement of the sprite is based on the cameras rotation.

Now my problem lies in choosing the sprite animation for either forward, sideways or backwards movement based on the direction vector but the direction vector defines different direction based on the camera orientation.

here’s the movement script

func movement():
	var direction = Vector3.ZERO
	var camera_basis = camera.get_global_transform().basis
	
	# clean up camera_basis to align with xz axis
	var camera_z = camera_basis.z
	var camera_x = camera_basis.x
	camera_z.y = 0
	camera_z = camera_z.normalized()
	camera_x.y = 0
	camera_x = camera_x.normalized()
	
	if Input.is_action_pressed("move_forward"):
		direction -= camera_z
		look_at(global_transform.origin + camera_z, Vector3.UP)
	if Input.is_action_pressed("move_back"):
		direction += camera_z
		look_at(global_transform.origin + camera_z, Vector3.UP)
	if Input.is_action_pressed("move_left"):
		direction -= camera_x
	if Input.is_action_pressed("move_right"):
		direction += camera_x
	
	if direction != Vector3.ZERO:
		direction = direction.normalized()
		
	target_velocity.x = direction.x * speed
	target_velocity.z = direction.z * speed
	target_velocity.y = 0
		
	# Moving the character
	velocity = target_velocity
	move_and_slide()

I’m pretty sure there’s a way to extract the movement direction from the direction and camera_basis but I’m struggling to come up with the equations to do so.

I was able to solve the issue by rotating the direction vector back to 0,0,0 with my camera pivot rotation!