Godot shotgun jump moving left when aiming down?

Godot Version

4

Question

I am making a godot shotgun game where you move with the shotgun but also attack eith it, I recently found that I move left when i am trying to move up, basically if my aim is too steep on either up or down i move left? heres my code

extends CharacterBody2D

@onready var node = $Node2d/Player
const SPEED = 300.0
const JUMP_VELOCITY = -500.0

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
var recoil_velocity = Vector2.ZERO
var recoil_duration = 0.2
var recoil_timer = 0.0
var recoil_active = false
var arc_factor = 1.0

func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta

if Input.is_action_just_pressed("ui_accept") and is_on_floor():
	velocity.y = JUMP_VELOCITY


var direction = Input.get_axis("ui_left", "ui_right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)


if recoil_active:

	global_position += recoil_velocity * delta


	recoil_timer -= delta
	if recoil_timer <= 0:
		recoil_velocity = lerp(recoil_velocity, Vector2.ZERO, arc_factor * delta)

		if recoil_velocity.length() < 1:
			recoil_active = false
			recoil_velocity = Vector2.ZERO

move_and_slide()


if Input.is_action_just_pressed("shoot"):
	var mousepointer = get_global_mouse_position()
	var recoil_direction = (global_position - mousepointer).normalized()

	recoil_velocity = recoil_direction * 1500  


	recoil_active = true
	recoil_timer = recoil_duration