Multiple idle animations
-
Hello, im trying to make multiple idle animations for my player, and other entities aswell based on previously moving direction, defaulting to idleDown, so my script is like this:
extends CharacterBody2D @export var speed: int = 150 @onready var animations = $AnimationPlayer func handleInput(): var moveDirection = Input.get_vector("ui_right", "ui_left", "ui_up", "ui_down") velocity = moveDirection*speed func updateAnimation(): var direction = "" var walk = "walk" var idle = "idle" var animation = "" if velocity.x < 0: direction = "Left" elif velocity.x > 0: direction = "Right" elif velocity.y < 0: direction = "Up" elif velocity.y > 0: direction = "Down" if velocity.x == 0 and velocity.y == 0: animation = idle + direction else: animation = walk + direction animations.play(animation) func _physics_process(delta): handleInput() move_and_slide() updateAnimation()
But well, its not working, if i default value direction to Down, it will use idleDown to reset animations, but other idle animations do not work, im quite new and dont know much but how do i do this
-
I believe the other idle animations do not work because the "direction" variable is not being properly set. You want to play an idle animation whenever the velocity of x and y is 0. But then just above it, your code tries to set "direction" depending on the velocities that are greater/lesser than 0. Does my explanation make sense? Velocity cannot be 0 in order to set a direction but at the same time it must be 0 to play the idle animation.
The solution is to make a variable that keeps track of the direction/velocity only while the object is moving.
Make a new variable like "last_velocity," then keep setting it to velocity IF velocity is not 0 (in handleInput). Then, you could use last_velocity to set direction (in updateAnimation).