Delay between button press and animation playing

Godot Version

4.4

Question

Hey guys, I have a problem with my player animation. When I press the Jump button there is a weird delay between me pressing it and the animation playing, for example when I walk and press jump the player jumps and for a moment it stays on the walking anmation before switching to the jump animation. The thing is I tested it on a separate project and it works flawlessly, I have no idea what the issue might be but if anyone has an idea as to why this might be happening i’d love to know.

Here is the code.

extends CharacterBody2D
#Variables
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var ap = $AnimationPlayer
@onready var sprite = $Sprite2D
var decelerate_on_jump_realease = 0.5 
var walk_speed = 100
var run_speed = 150
var acceleration = 0.1 
var jump_force = -400.0
var deceleration = 0.1
var health = 4
var hit_by_enemy = false
var time_in_seconds = 0.5
#------------------------
func _physics_process(delta):
	#Gravity
	if !is_on_floor():
		velocity.y += gravity * delta
	#Jumping
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_force
	if Input.is_action_just_released("jump") and velocity.y < 0:
		velocity.y *= decelerate_on_jump_realease
	#Run
	var speed
	if  Input.is_action_pressed("run"):
		speed = run_speed
	else:
		speed = walk_speed
	#Left/Right Movement
	var direction := Input.get_axis("left", "right")
	velocity.x = move_toward(velocity.x, direction * speed, walk_speed * acceleration)
	if direction != 0:
		sprite.flip_h = (direction == -1)
	
	move_and_slide()
	update_animations(direction)
#------------------------
func update_animations(direction):
	if is_on_floor():
		if direction == 0:
			ap.play("Idle")
		else:
			if Input.is_action_pressed("run"):
				ap.play("Run")
			else:
				ap.play("Walk")
	else:
		if velocity.y < 0:
			ap.play("Jump")
		elif velocity.y > 0:
			ap.play("Fall")
#------------------------
func hit(posx):
	hit_by_enemy = true
	velocity.y = jump_force * 0.7
	if position.x < posx:
		velocity.x = 200
	elif position.x > posx:
		velocity.x = -200
	$AudioStreamPlayer2D.play()
	if hit_by_enemy == true and health>3:
		health -= 1
		$HUD/health_full.set_self_modulate(Color(0,0,0,0))
		hit_by_enemy = false
	elif hit_by_enemy == true and health>2:
		health -= 1
		$HUD/health_three_fourth.set_self_modulate(Color(0,0,0,0))
		hit_by_enemy = false
	elif hit_by_enemy == true and health>1:
		health -= 1
		$HUD/health_half.set_self_modulate(Color(0,0,0,0))
		hit_by_enemy = false
	elif hit_by_enemy == true and health>0:
		health -= 1
		$HUD/health_quarter.set_self_modulate(Color(0,0,0,0))
		hit_by_enemy = false
	if health == 0:
		await get_tree().create_timer(time_in_seconds).timeout
		get_tree().change_scene_to_file("res://scenes/death_screen.tscn")
	Input.action_release("left")
	Input.action_release("right")