Enemy attack animation problem

Problem

Hi, I’m new to Godot and I made a game that you can run and kill your enemy with melee attack. And after I finished the player attacking function, I wanted to add a enemy attack function. So I wrote these basic lines:

if can_attack == true and dealing_with_damage == false:
		$Detection_area/CollisionShape2D.disabled = true
		$AttackTimer.start()
		$AnimatedSprite2D.play("attack")
		global.is_enemy_attacking = true
	
func _on_attack_timer_timeout():
	global.is_enemy_attacking = false
	$Detection_area/CollisionShape2D.disabled = false
	if can_attack == true:
		return attack()

Then the code actually worked and my player started to taking damage. But the enemy’s attacking animation didn’t worked. More precisely, the enemy stuck in only one frame of the animation. If you help, thank you in advance! :grinning:
(Note: $Detection_area/CollisionShape2D.disabled means enemy can’t move while attacking)

your attack animation might not have “loop” enable or you might spamming the attack animation

Well, how to enable it or how to avoid spamming?

you can go to your animation and turn on the loop (it is at the left corner of the animation bar)
if that isn’t the case add a flag to your if condition like attacking == true:

I tried your solution about loop but it didn’t work. At the other case, what do you mean by “add a flag”?(idk much of godot like I said)

try this:

var attacking = false #add this on top of all the func
###
if can_attack == true and dealing_with_damage == false and attacking == false:
		$Detection_area/CollisionShape2D.disabled = true
		$AttackTimer.start()
		$AnimatedSprite2D.play("attack")
		global.is_enemy_attacking = true
		attacking = true
	
func _on_attack_timer_timeout():
	attacking = false
	global.is_enemy_attacking = false
	$Detection_area/CollisionShape2D.disabled = false
	if can_attack == true:
		return attack()

I tried it but now enemy plays the animation for a very very little bit of time then backs to normal.

that might be because of your movement animation and idle animation

So I need to add :

if attacking == false:
   $AnimatedSprite2D.play("run")



right?

correct

Ok but it didn’t worked again

is it doing nothing or is it doing something unintended

Oh, sorry! It’s just because of I misspelled someting. Your solution is working. Thanks!

1 Like

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