There is a problem with animation playback

Godot Version

4.2.1 stable

Question

Good evening, everyone. there is a problem with animation playback. I have characterbody2d, animatedsprite2d is connected to it. the problem is that when the buttons are pressed, the sprite changes, but the animation does not play. maybe the problem is in the code

extends CharacterBody2D

@export var speed = 200

func get_input():
	var input_direction = Input.get_vector("left", "right", "up", "down")
	velocity = input_direction * speed

func _physics_process(delta):
	get_input()
	move_and_slide()
	

@onready var _animated_sprite = $AnimatedSprite2D

func _process(_delta):
	if Input.is_action_pressed("right"):
		_animated_sprite.play("right")
	if Input.is_action_pressed("left"):
		_animated_sprite.play("left")
	if Input.is_action_pressed("up"):
		_animated_sprite.play("up")
	if Input.is_action_pressed("down"):
		_animated_sprite.play("down")
	else:
		_animated_sprite.stop()

this code mean if you dont press “down” button, it will always stop the animation
you need elif

to fix
you do it like this:

func _process(_delta):
	if Input.is_action_pressed("right"):
		_animated_sprite.play("right")
	elif Input.is_action_pressed("left"):
		_animated_sprite.play("left")
	elif Input.is_action_pressed("up"):
		_animated_sprite.play("up")
	elif Input.is_action_pressed("down"):
		_animated_sprite.play("down")
	else:
		_animated_sprite.stop()
1 Like

thanks bro

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.