My character doesn't animate when walking diagonally

Godot Version

extends CharacterBody2D
const SPEED = 100.0
@onready var anim = get_node(“AnimatedSprite2D”)

func _ready():
anim.play(“Frente”)

func _physics_process(delta):
#Recebe a direção de acordo com a tecla pressionada
var direction_X = Input.get_axis(“passo_esquerda”, “passo_direita”)
var direction_Y = Input.get_axis(“passo_cima”, “passo_baixo”)
#Move na linha X (esquerda, direita)
if direction_X:
velocity.x = direction_X * SPEED
#Animação de andar (lado)
if Input.is_action_pressed(“passo_esquerda”):
anim.play(“Esquerda_passo”)
if Input.is_action_pressed(“passo_direita”):
anim.play(“Direita_passo”)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
#Animação de idle (lado)
if Input.is_action_just_released(“passo_esquerda”):
anim.play(“Esquerda”)
if Input.is_action_just_released(“passo_direita”):
anim.play(“Direita”)
#Move na linha Y (cima, baixo)
if direction_Y:
velocity.y = direction_Y * SPEED
#Animação de andar (frente e costas)
if Input.is_action_pressed(“passo_cima”):
anim.play(“Costas_passo”)
if Input.is_action_pressed(“passo_baixo”):
anim.play(“Frente_passo”)

Question

It’s my first time trying to make a game, and I managed to make my character walk and perform the walking animation. However, when he moves diagonally, the animation stops, and it becomes just a frame moving. How can I fix this? Or, if anything, how can I prohibit diagonal movement?

I think your issue is that all your if statements are separate instead of being in one block. That means that if all the buttons are pressed down, all the animations are played one after another, which resets them every frame. You should use elif blocks so that only one animation is played at a time.

1 Like

it worked! Thank you!!!

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