Need help adding animations to my movement

Godot 4.2

Hey, total newbie here trying to learn by making my own games.
So far I’ve made all the sprites for my main character and the animations work great, I have added them all to Godot. Now I’ve made my character move around the screen but currently he just holds the idle stance and doesn’t use the animations, he just floats around. I want to make it so that he plays the correct animation when going north, south, east, west. Here is my script so far.

extends CharacterBody2D

const SPEED = 100.0
const ACCEL = 2.0

var input: Vector2
var animated_sprite: AnimatedSprite2D

func get_input():
input.x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
input.y = Input.get_action_strength(“ui_down”) - Input.get_action_strength(“ui_up”)
return input.normalized()

func _process(delta):
var playerInput = get_input()

	velocity = lerp(velocity, playerInput * SPEED, delta * ACCEL)

	move_and_slide()

The animations I have made are called

walk_north, idle_north
walk_south, idle_south
walk_east, idle_east
walk_west, idle_west

I did realize later that I could have just used 1 animation for east and then used the fliph thing to face it the other way but I had already made the seperate animations and added them. So how do I get these animations to play when moving the correct direction or not moving at all??

All help is appreciated.

What I did for mine is I made a variable for animation direction and had it change correspondingly with each directional change. Example:

if Input.is_action_pressed("move_down"):
		position.y += SPEED
		animationDirection = "down"

Then I called those variables to choose the animation. Here’s an example:

if velocity.x == 0:
		if animationDirection == "left":
			animation = "IDLE_L_FRONT"

I am also a newbie, so it may not be perfect, but it does seem to work.

I learned about animated sprites in the dodge the creeps tutorial. This page specifically helped me a lot https://docs.godotengine.org/en/stable/getting_started/first_2d_game/03.coding_the_player.html

Hope that helps!

unfortunately this does not work for me. I have gone through several different tutorials and am getting more confused as I go because everyone codes their movement and animations differently. I need to figure out a consistent way to do this. I have 4 directions, and 4 animations made to go along with them. I just need to implement them…

Right now my character will move all directions, and when i press right he will play the walk animation for that. But I can’t figure out how to get every direction working…

This is the script thus far.

extends CharacterBody2D

var speed = 400

@onready var animated_sprite = $AnimatedSprite2D

func _physics_process(delta):
var direction = Input.get_vector(“ui_left”, “ui_right”, “ui_up”, “ui_down”)
velocity = direction * speed

func _process(delta):

if Input.is_action_pressed("ui_right"):
	animated_sprite.play("walk_east")
else : animated_sprite.play("idle_east")





move_and_slide()