I have a hurt function of the enemy, which will be called whenever the player hit the hurt box and then play a specific animation based on the player position (I use the animationTree node with animationBlendState2D inside the animationBlendTree in order to play the animations btw).
My goal is simple, play the hurt animation based on the player location, but comes up the problematic when the player changes his self position while the animation is still playing, which will make the animationTree play over again another hurt animation from the beginning because the player position is changes.
Pardon me, maybe the codes can summarize it better:
is_hurt = false
func _physics_process(delta: float) -> void:
if is_dead == false:
knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
knockback = move_and_slide(knockback)
if is_hurt == false:
if is_attacking == false:
if _player != null:
state = MOVE
#etc (ask for more detail)
if ! is_attacking && position.distance_to(player.position) < attack_distance:
is_attacking = true
if is_attacking == true:
state = ATTACK
if is_hurt == true:
state = HURT
if is_dead == true:
state = DEATH
velocity = move_and_slide(velocity)
start.health -= area.damage
knockback = area.knockback_vector * 250
is_attacking = false
is_moving = false
is_hurt = true
$Sprite.visible = true
$Sprite2.visible = false
is_hurt = false
To my way of thinking, the way those functions work, when the player hit the hurtbox,
is_hurtwill = true in order to change the state to HURT (
hurt_state()). Further more, on the
hurt_state() function, the animationTree will be called with the code
statemachine.travel("Hurt"). When the animation is finished,
is_hurt_timeout() will be actived and return
is_hurt equal to false.
As you can see, if the player change himself position, for instance, runs from right to left of the enemy when the animation hasn't play to an end, because the animationState is looking at player location, as a result, the animationTree will play another animation (hurt left) from the beginning, if the player run around the enemy, the function
is_hurt_timeout() at the last frame won't be called and the animations will be played for eternity.
Hope that would make up your mind and obviously, very sorry for my horrible English. It would be great if you guys could let me know anything that you are still struggle about, I would love to answer all of them, at the end, hope you have a wonderful day.