Today I wanted to add enemy animations to my game.
Enemy Header file
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = SkeletalMesh)
USkeletalMesh* ModelMesh;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = SkeletalMesh);
UClass* AnimBp;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = State)
FName EnemyState;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = State)
bool FacingRight;
Enemy CPP file
void ACompetitionEnemy::BeginPlay()
USkeletalMeshComponent* EnemyMesh = GetMesh();
EnemyMesh->SetSkeletalMesh(ModelMesh);
EnemyMesh->SetAnimationMode(EAnimationMode::AnimationBlueprint);
EnemyMesh->SetAnimInstanceClass(AnimBp);
EnemyMesh->RegisterComponent();
EnemyMesh->SetCollisionProfileName(CollisionProfile.Name, true);
EnemyMesh->OnComponentBeginOverlap.AddDynamic(this, &ACompetitionEnemy::OnOverlapStart);
This code works the same as when I added animations to my player, the mesh and animation blueprint are set in the blueprint and added in Begin Play.
void ACompetitionEnemy::SpawnPunch()
{
EnemyState = "WindingPunch";
...
if (ACompetitionEnemy::GetActorLocation().Y > StartLocal.Y)
{
FacingRight = true;
}
else if (ACompetitionEnemy::GetActorLocation().Y < StartLocal.Y)
{
FacingRight = false;
...
GetWorldTimerManager().SetTimer(PunchTimerHandle, this, &ACompetitionEnemy::ThrowPunch, 1.0f, false);
...
void ACompetitionEnemy::ThrowPunch()
{
EnemyState = "ThrownPunch";
Now my ‘SpawnPunch’ function will also check the direction the enemy is facing for use in the animation blueprint. Also “ThrowPunch” can now be set as the enemy state for managing collisions.
void ACompetitionEnemy::Damage(float DamageValue)
{
health -= DamageValue;
if (health <= 0)
{
Destroy();
}
Now the ‘damage’ function will accept a float.
void ACompetitionEnemy::OnOverlapStart()
if (ACompetitionCharacter* Player = Cast<ACompetitionCharacter>(OtherActor))
{
if (EnemyState == "ThrownPunch")
{
if (Player->State == "Blocking")
{
Player->Damage(10.f);
}
else
{
Player->Damage(25.f);
}
Collisions are now managed in the corresponding C++ file, and calls the opponents damage if a hit occurs.
Player CPP file
void ACompetitionCharacter::OnOverlapStart()
{
if (ACompetitionEnemy* Enemy = Cast<ACompetitionEnemy>(OtherActor))
{
if (State == "Punching")
{
Enemy->Damage(20.f);
}
else if (State == "SuperPunch")
{
Enemy->Damage(50.f);
void ACompetitionCharacter::Damage(float DamageAmount)
{
health -= DamageAmount;
if (health <= 0)
{
GetWorldTimerManager().ClearAllTimersForObject(this);
Here the player damage and collision functions nearly identical to that of the enemy, but checks for the state ‘SuperPunch’ to deal more damage.
Enemy Animation Blueprint

Here you can see my enemy punching left and right with animations.