0 votes

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

enum{
DEATH
}

func _process(delta):
 match state:
    DEATH:
        death_state()

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()
    anim_tree.set("parameters/Death/death/blend_position",Position)
    state = DEATH
    velocity = Vector2.ZERO
    yield(get_tree().create_timer(0.1), "timeout")
    Position = Vector2.ZERO
 velocity = move_and_slide(velocity)

func _on_start_no_health():
 is_dead = true

func death_state():
 $Sprite.visible = true
 $Sprite2.visible = false
 _player = null
 $Hurtbox/CollisionShape2D.disabled = true
 $shadow.visible = false
 velocity = Vector2.ZERO
 anim_tree.set("parameters/Death/TimeScale/scale", 1.125)
 state_machine.travel("Death")

func is_dead():
 queue_free()

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.

Godot version godot 3.3
in Engine by (56 points)
edited by

So the problem is on the enemy or their interaction with the player? When the enemy is in their 'Death' state, verify that you also disable anything else that may trigger again the animation; e.g. disable the collision shape (done), set_physics_process(false) (also set_process(false)) and so on.

It looks like the _process function will continually call death_state()? I'm assuming you don't need to _process when dead and you just desire the animation to finish and call is_dead().

My god! Why haven't I thought about it earlier, your reply really made my day, thanks a million for the help!

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.