Godot Version
4.2.1
Question
I am very new to coding/game development and have what is probably a very simple question. I want my character to play a different idle animation based on what direction they were last moving. I have walking animations working but I’m struggling with getting the correct idle animation to play.
I will post the script below. I actually know why this script doesn’t work(variable direction is set to “down” by default), I just can’t figure out how to make it work. Your help would be much appreciated!
extends CharacterBody2D
@export var speed = 150
func get_input():
var input_direction = Input.get_vector(“move_left”, “move_right”, “move_up”, “move_down”)
velocity = input_direction * speed
func update_animation():
var is_moving = velocity.length_squared() > 0
var direction = “down”
if is_moving:
if velocity.x < 0: direction = “left”
elif velocity.x > 0: direction = “right”
elif velocity.y > 0: direction = “down”
elif velocity.y < 0: direction = “up”
$player_sprite.play("walk_" + direction)
else:
$player_sprite.play("idle_" + direction)
func _physics_process(delta):
move_and_slide()
get_input()
update_animation()