Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | mashiyatz |
Hello,
I’m working on a simple sidescroller (Godot 3.2.2) and have created an enemy character. He has an animation for idle, walking, and taking damage, which I prepared using the AnimatedSprite node. I want his animation to switch to “damaged” every time his hitbox overlaps with the player’s sword, but it seems the animation won’t play, even though the signal works.
In my _physics_process
code, I have the following. As you can see, I have created three states corresponding to the type of action.
match state:
IDLE:
velocity = velocity.move_toward(Vector2.DOWN * GRAVITY, ACCELERATION * delta)
animatedSprite.play("idle")
seek_player()
CHASE:
if player != null:
direction = (player.global_position - global_position).normalized()
velocity = velocity.move_toward(Vector2(MAX_SPEED * direction.x, GRAVITY), ACCELERATION * delta)
animatedSprite.play("walk")
animatedSprite.flip_h = velocity.x < 0
elif player == null:
state = IDLE
DAMAGED:
print('ouch!')
velocity.x = 0
animatedSprite.play("damaged")
seek_player()
move_and_slide(velocity)
Here is my seek_player() function and the signals I’m using to manage the states:
func seek_player():
if player != null:
state = CHASE
else:
state = IDLE
func _on_HurtBox_area_entered(area):
state = DAMAGED
func _on_HurtBox_area_exited(area):
state = IDLE
func _on_PlayerDetection_body_entered(body):
player = body
func _on_PlayerDetection_body_exited(body):
player = null
Basically, if the enemy sees the player, he will start walking. If the player escapes his view, he will be idle. Finally, if the enemy is hit with the sword, he will appear to take damage. But while the DAMAGE state runs (I see the print statement “ouch!” in the console), the animation never plays.
But if I remove the play(“walk”) line from the CHASE state, the “damaged” animation plays as expected - however, the enemy can no longer walk.
I’ve been trying to figure this out over the last few days and can’t tell if I’m missing something simple or what! I’d appreciate any help! Let me know if you need more context.