8 directional movement and animation Godot 4

Godot Version
4.2.2

Question
How to make an animated diagonal movement in this script, do you have any suggestions? (It is not necessary to change this particular script, if you have your own script for this. I will be glad of any help!):

extends CharacterBody2D

const speed = 100
var current_dir = “none”

func _ready():
$AnimatedSprite2D.play(“Down_Idle”)

func _physics_process(delta):
player_movement(delta)

func player_movement(delta):
if Input.get_action_strength(“right”):
play_anim(1)
current_dir = “right”
elif Input.get_action_strength(“left”):
play_anim(1)
current_dir = “left”
elif Input.get_action_strength(“down”):
play_anim(1)
current_dir = “down”
elif Input.get_action_strength(“up”):
play_anim(1)
current_dir = “up”
else:
play_anim(0)

var input_vector = Vector2.ZERO

input_vector.x = Input.get_action_strength(“right”) - Input.get_action_strength(“left”)
input_vector.y = Input.get_action_strength(“down”) - Input.get_action_strength(“up”)
input_vector = input_vector.normalized()

if input_vector:
velocity = input_vector * speed
else:
velocity = input_vector

move_and_slide()
func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D

if dir == “right”:
anim.flip_h = false
if movement == 1:
anim.play(“Right”)
elif movement == 0:
anim.play(“Right_Idle”)
if dir == “left”:
anim.flip_h = true
if movement == 1:
anim.play(“Right”)
elif movement == 0:
anim.play(“Right_Idle”)

if dir == “down”:
anim.flip_h = true
if movement == 1:
anim.play(“Down”)
elif movement == 0:
anim.play(“Down_Idle”)
if dir == “up”:
anim.flip_h = true
if movement == 1:
anim.play(“Up”)
elif movement == 0:
anim.play(“Up_Idle”)

Assuming you have diagonal animations already, I would set conditions to play the diagonal animations based on the direction your character is currently moving.

I used this a 3d sprite plane, to switch animations based on the players rotation. However, for your case I don’t think you need rotation, just direction of movement. They key is to play the diagonal animations based on direction.

something like this;>

func determine_direction(input_vector: Vector2):
if input_vector.x > 0 and input_vector.y > 0:
current_dir = “down_right”
elif input_vector.x > 0 and input_vector.y < 0:
current_dir = “up_right”
elif input_vector.x < 0 and input_vector.y > 0:
current_dir = “down_left”
elif input_vector.x < 0 and input_vector.y < 0:
current_dir = “up_left”
elif input_vector.x > 0:
current_dir = “right”
elif input_vector.x < 0:
current_dir = “left”
elif input_vector.y > 0:
current_dir = “down”
elif input_vector.y < 0:
current_dir = "up"T)

play_anim(1)

func play_anim(movement):
match current_dir:
“right”:
animated_sprite.flip_h = false
animated_sprite.play(“Right” if movement == 1 else “Right_Idle”)
“left”:
animated_sprite.flip_h = true
animated_sprite.play(“Right” if movement == 1 else “Right_Idle”)
“down”:
animated_sprite.flip_h = false
animated_sprite.play(“Down” if movement == 1 else “Down_Idle”)
“up”:
animated_sprite.flip_h = false
animated_sprite.play(“Up” if movement == 1 else “Up_Idle”)
“down_right”:
animated_sprite.flip_h = false
animated_sprite.play(“Down_Right” if movement == 1 else “Down_Right_Idle”)
“down_left”:
animated_sprite.flip_h = true
animated_sprite.play(“Down_Left” if movement == 1 else “Down_Left_Idle”)
“up_right”:
animated_sprite.flip_h = false
animated_sprite.play(“Up_Right” if movement == 1 else “Up_Right_Idle”)
“up_left”:
animated_sprite.flip_h = true
animated_sprite.play(“Up_Left” if movement == 1 else “Up_Left_Idle”)