Different Animations For Death

I wanted to have the AI character have different animations if they were shot from the front, or from behind. I don’t know if this is the best way, but this is how I decided to do it.

First I set up the AnimGraph to first check if the player is shot from behind, and play different animations.

Then I set the event graph to get the value (from a UFUNCTION I created in the code (see below)). I made sure to set the IsShotFromBehind variable before the IsDead variable, so that is doesn’t play the death animation before checking which death animation to play.

I created a UFUNCTION in the Shooter Character header and cpp file to get the Shot from behind boolean as so

UFUNCTION(BlueprintPure)
bool IsShotFromBehind() const;

bool AShooterCharacter::IsShotFromBehind() const
{
return bShotFromBehind;
}

Now all I needed to do was create a member variable to store the value, and a function to set the variable

private:
bool IsPlayerBehindEnemy(AActor* Shooter) const;

bool bShotFromBehind = false;

Using vectors, I calculated the vector from the Owner Character, to the Actor causing the damage. If this vector had a Yaw value greater than the absolute value of 90 degrees, then the Owner had been shot from behind. Otherwise the Owner was shot from the front.

bool AShooterCharacter::IsPlayerBehindEnemy(AActor* Shooter) const
{
FVector ShooterLocation = Shooter->GetActorLocation();
FVector OwnerLocation = GetOwner()->GetActorLocation();
FVector FromOwnerToShooter = ShooterLocation - OwnerLocation;
float AngleFromFront = FMath::Abs(FromOwnerToShooter.Rotation().Yaw);
UE_LOG(LogTemp, Warning, TEXT(“Angle from front to Shooter is %f”), AngleFromFront);

if (AngleFromFront > 90.f)
{
return true;
}
else
{
return false;
}
}

EDIT: I realised after added the AI Component to make the opponent always focus on the player character, I was setting the above AngleFromFront based on the Global rotation, not the local rotation. Replacing the above calculation with this one will fix this error.

float AngleFromFront = FMath::Abs(FromOwnerToShooter.Rotation().Yaw - GetActorRotation().Yaw);

Then I just set the bShotFromBehind variable during the TakeHealth function, before changing the isDead variable (once again to stop the death animation playing before checking the direction of the shot).

float AShooterCharacter::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
float DamageToApply = Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
DamageToApply = FMath::Min(Health, DamageToApply);
Health -= DamageToApply;
UE_LOG(LogTemp, Warning, TEXT(“Health is %f”), Health);
if (Health == 0)
{
bShotFromBehind = IsPlayerBehindEnemy(DamageCauser);
bDead = true;
}
return DamageToApply;
}

3 Likes

Awesome job with the different animations. Have you being messing around with UE5?

Not yet. I’m still getting use to UE4, though I will probably check it out down the line.

Privacy & Terms