Animation Help - Trying to get Attack animation to play while walking

extends CharacterBody2D
@onready var player_anim = $AnimatedSprite2D


var player_speed := 75 
var player_direction := 0.00 #-1 or 1 direction
var is_attacking = false
var is_walking = false

func _physics_process(delta):
	player_direction = 0 #resets direction to 0 when we let go of input

#problems:
#need a loop that returns animation to idle instead

	if Input.is_action_pressed("move_left"):
		player_direction = -1
		player_speed * player_direction
		is_walking = true
		is_attacking = false
		print(player_direction)
		if player_direction < 1:
			player_anim.flip_h = true
			player_anim.play("walk")
	elif Input.is_action_pressed("move_right"):
			player_direction = 1
			player_speed * player_direction
			is_walking = true
			is_attacking = false
			print(player_direction)
			if player_direction == 1:
				player_anim.flip_h = false
				player_anim.play("walk")
	elif Input.is_action_just_pressed("attack"):
		player_direction = 0
		player_speed * player_direction
		is_walking = false
		is_attacking = true
		if is_walking == false and is_attacking == true:
			player_anim.play("attack")
	else:
		player_anim.play("idle")

	

	velocity.x = player_speed * player_direction
	move_and_slide()

4.2.2

Question

I need help with my player animation. I’m trying to get the attack animation to play when pressing the attack input while also stopping the movement of the player. I tried different things to get it to work, but when I press the attack input, the animation slightly stutters and goes back to either walking or idle. I also tried attacking while moving but it still does the same thing. How can I get this to loop correctly? Any tips on fixing this would be greatly appreciated!

Try this:

extends CharacterBody2D
@onready var player_anim = $AnimatedSprite2D


var player_speed := 75 
var player_direction := 0.00 #-1 or 1 direction
var is_attacking = false
var is_walking = false

func _physics_process(delta):
	player_direction = 0 #resets direction to 0 when we let go of input

#problems:
#need a loop that returns animation to idle instead

	if Input.is_action_pressed("move_left"):
		player_direction = -1
		player_speed * player_direction
		is_walking = true
		#is_attacking = false
		print(player_direction)
		if player_direction < 1 and !is_attacking:
			player_anim.flip_h = true
			player_anim.play("walk")
	elif Input.is_action_pressed("move_right"):
			player_direction = 1
			player_speed * player_direction
			is_walking = true
			#is_attacking = false
			print(player_direction)
			if player_direction == 1 and !is_attacking:
				player_anim.flip_h = false
				player_anim.play("walk")
	elif Input.is_action_just_pressed("attack"):
		attack()
	else:
		if !is_attacking: player_anim.play("idle")

	velocity.x = player_speed * player_direction
	move_and_slide()

func attack():
   var attack_duration = 1.0 #Adjust it as need
   player_direction = 0
   player_speed * player_direction
   is_attacking = true
   player_anim.play("attack")
   await get_tree().create_timer(attack_duration).timeout
   is_attcking = false

One more tip, for make the codes easy, you must need to create a function that will play animations, and it will get call at the end of the process.

1 Like

Thank you for this! The character still does not attack while I walk however I’m going to use what you gave me and tweak some stuff to get this to work.

1 Like