First off, I want to say thank you to anyone who still answering beginner's questions, you guys did a great job and I really appreciate about that.
Sorry, I will get into the main point, I have a dead function of the enemy, which will be called whenever the enemy hp <= zero 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 dead animation based on the player location, but comes up the problematic when the player changes his self position while the animation is still playing (there are many frames for the death) which will make the animationTree play over again another dead animation from the beginning because the player position is changes.
Pardon me, maybe the codes can summarize it better:
var is_dead = false
func _physics_process(delta: float) -> void:
if is_dead == false:
knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
knockback = move_and_slide(knockback)
#etc(ask if you want to see it)
if is_dead == true:
var Position = position.direction_to(player.position).normalized()
state = DEATH
velocity = Vector2.ZERO
Position = Vector2.ZERO
velocity = move_and_slide(velocity)
is_dead = true
$Sprite.visible = true
$Sprite2.visible = false
_player = null
$Hurtbox/CollisionShape2D.disabled = true
$shadow.visible = false
velocity = Vector2.ZERO
To my way of thinking, the way those functions work, when is dead = true, the velocity will = to Vector2.ZERO in order stop the enemy movement, further more, the state will = to Death, which will called the function deathstate() whenever the enemy health is <= 0 like I said earlier, thus, it will play the animationTree with the code "statemachine.travel("Death")", when the animation is finished, the enemy will be deleted.
As you can see, if the player change himself position, for instance, runs from right to left 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 (death left) from the beginning, if the player run around the enemy, the function "is_dead()" at the last frame won't be called and the enemy will be existed for eternity.
Hope that would make up your mind and obviously, very sorry for my horrible English. At the end, I just want to say thank you for all of your efforts, I don't really have much money but still, everyone keep helping the beginners for free, 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. Finally, thank you and hope you guys have a wonderful day.