Hello I use Godot 4 and I´m rather new to the game development. So I struggle with idle animations. I already did do IdleUp, IdleDown etc. But it wont work. And even if I only do one animation the IdleDown animation it will never play the actual animations
Here is the code:
extends CharacterBody2D
@export var speed: int = 35
#@export bedeutet das man es sieht im Interface #var benennt ein value und int sagt das nur ganzzählige Werte zugewiesen werden können @onready var animations = $AnimationPlayer
#@onready bedeutet
func updateAnimation():
if velocity.length() == 0:
animations.stop()
else:
var direction = “Down”
if velocity.x < 0: direction = “Left”
elif velocity.x >0: direction =“Right”
elif velocity.y < 0: direction = “Up”
animations.play ("Walk" + direction)
if velocity == Vector2.ZERO:
animations.play ("IdleDown")
An idle animation plays automatically if the player is not moving for a certain amount of time.
Therefore I would add a Timer for the player scene.
It runs the moment it detects that velocity is 0 (aka: the player is not moving).
Then you can hook onto the timer.finished signal and then play the animation that you want.
If you set the wait_time of the timer to be 2seconds, it would mean the idle animation starts 2seconds after not moving.
Of course, the timer will be reset and the animation stopped if the velocity is != 0.
Here’s a modified version of your update animation function. I moved the direction variable to be global, and it saves the direction you moved in last.
var direction = "Down"
func updateAnimation():
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 == Vector2.ZERO:
animations.play("Idle" + direction)
else:
animations.play("Walk" + direction)