How to play Death Animation whenever the enemies Health is 0? (In Blueprints or C++ and spawn again with the AI Controller)

Please just guide me how can I do this :pleading_face:

When you know that your character is dead there are your options :

  1. Just turn on ragdoll physics and it will fall by itself. Also, add some small force so it will fall in the right direction.
  2. Play certain animation via AnimMontage and make sure that characters stay at this pose or enable ragdoll afterward.

To track down when Health <= 0 you should add damage via some function that will check health amount after applying damage.

1 Like

To play the montage, add an animation slot between the state machine and the pose (in the animation blueprint).
Then you can use Play Montage from blueprint (or c++). This node as a “animation completed” pin that you can use to remove the pawn after he’s dead or add some kind of effect.

Edit:
To play from C++ and use the montage callbacks, search for this class in the Visual Studio solution:
UPlayMontageCallbackProxy. You can base your code from there.

1 Like

To prevent bugs:
Create a statemachine called “dead” in the animation.
Whenever the character is actually dead (so after ragdoll or animation plays out) toss the animation in this state.
Usually with a pose snapshot so it costs near nothing.

1 Like

So basically the problem is that I already have a idle and running animation attached to the enemies mesh, and I can’t put another so the death animation isn’t really working.


(this is the screenshot of the death animation blueprint ^)

That makes 0 sense.
You aren’t limited to anything - there’s no reason you can’t just add another state and transition to it.

Okay sorry forget that stupid thing I said, can you tell me how do I switch states?
image

There’s a learn tab on the epic website.

You should access it and look for animation tutorials.

It will teach you how it works and the basics you need to know to use the engine.

Also this series.

2 Likes

MostHost_LA is absolutely right. You can’t post a question every time you have a problem. Make some search first. I’ve never used double state machines but possibly I know how to do that. You have to cache the poses that come out from the state machine. How do I know that? Because I’ve watched countless videos about animation. Have you completed the courses from the Unreal Learning Portal? That’s a good place to start.

But you don’t need multiple state machines for the death animation. It’s a lot simple to play a montage when the vampire dies.

You just have to add a animation slot between the state machine and the Output Pose. If a montage is playing on that slot, it will override de animation that comes out from the state machine.

You can play the montage from your blueprint with Play Montage.
Unreal Engine 4 - Animation Montage Setup - YouTube

1 Like

@SunbeamOut @Limanima @MostHost_LA Okay sorry for just disturbing you guys with my silly questions :pensive:

  1. I created a Dead State in the BP_Enemy_Animation Blueprint
    image

image

  1. Transition from Idle/Walk to Dead State.
    image
  1. Changed the Tick Event in BP_Enemy.
  1. Changed the BeginPlay Event in BP_Enemy.
    image
  1. Commented this part of my Enemy.cpp file so that it doesn’t interfere.
    image

References: YouTube tutorial I followed: https://youtu.be/naBMp4EqujM

But the problem I’m facing now is: Enemy is not dying or playing the animation and not getting destroyed.

@MostHost_LA @Limanima @SunbeamOut Please reply!!! I’ve Been waiting for almost forever. Merry Christmas btw :slight_smile:

The only thing I didn’t see in your code is the damage being dealt. Where do you change Enemy health?

You need to try to resolve errors on your own.
Where do you set up IsDead variable and deal damage? Check this two points

In the Enemy.cpp file.
image

In the BP_Enemy_Animation.
image

When is AEnemy::DealDamaga() called? Is “Health” in C++ the same variable as “Enemy Health” in Blueprint?

Okay thanks I figured everything out. The animation is playing properly, the enemy gets destroyed after that. But now can you tell me how to respawn the enemy after he dies? Checked the UE4 documentation, couldn’t find anything helpful.

I call the Spawn Enemy Event after the Destroy Actor function is called, but then when I hit play the Game crashes after 3-4 seconds or so.

Okay now the Game is not crashing cause I added a Delay after Destroy Actor and then called the Spawn Enemy.

But now the problem is that they are not spawned in the map, they are spawned in the world. So they are not visible, what should I do now?

@Limanima Hey it’s been long since you replied to this post.

Sorry.
First, if you are respanwing the enemy after he’s dead, why are you destroying him in the first place if you are going to revive him?
Just respawn him, I don’t see the need to destroy the actor.

Second, where do you want to respawn the enemy? You are using “GetActorTransform”, I’m not sure what will this return if you have destroyed the actor. Maybe someone on the forum knows what happens.

You have to tell somewhere the respawn location. Maybe you will have to add respawn points, because if it’s random you will never know where it will respawn, on top of a wall for instance.

1 Like