Creating AOE Damage in Unreal Engine with C++: A Step-by-Step Guide

Rambod
4 min readJan 28, 2023

--

Unreal Engine is a powerful game development engine that allows developers to create complex and visually stunning games. One of the many features that Unreal Engine provides is the ability to create area of effect (AOE) damage. In this article, we will be discussing how to create AOE damage in a C++ class in Unreal Engine using the example source code provided.

Projectile.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AProjectile.generated.h"

UCLASS()
class YOURPROJECTNAME_API AAProjectile : public AActor
{
GENERATED_BODY()

public:
AAProjectile();

protected:
virtual void BeginPlay() override;

public:
virtual void Tick(float DeltaTime) override;
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

private:
UPROPERTY(EditAnywhere, Category = "AOE Damage")
float Damage;

UPROPERTY(EditAnywhere, Category = "AOE Damage")
float AoeRange;

UPROPERTY(EditAnywhere, Category = "AOE Damage")
UParticleSystem* HitParticles;

UPROPERTY(EditAnywhere, Category = "AOE Damage")
USoundBase* HitSound;

UPROPERTY(EditAnywhere, Category = "AOE Damage")
TSubclassOf<UCameraShake> HitCameraShakeClass;
};

This is just an example, you may need to adjust it to your own specific project.

In this example, the AProjectile class inherits from the AActor class and uses the UCLASS macro to generate the necessary boilerplate code. It has several UPROPERTY variables that are editable in the editor, such as Damage, AoeRange, HitParticles, HitSound, and HitCameraShakeClass.

The class also has a function OnHit that takes certain parameters, this function is responsible for handling the collision of the projectile with another actor, as well as applying damage, spawning hit particles, and playing hit sound, shake the camera, and destroying the projectile.

It’s important to note that you’ll need to include the necessary headers for the UParticleSystem, USoundBase, and UCameraShake classes, as well as add the appropriate #include statements for the UGameplayStatics and UPrimitiveComponent classes in the cpp file for this header file to be able to be compiled successfully.

Projectile.cpp

#include "AProjectile.h"
#include "Engine/World.h"
#include "GameFramework/DamageType.h"
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"
#include "Components/PrimitiveComponent.h"

void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
// Get the owner of the projectile
AActor* MyOwner = GetOwner();
if (MyOwner == nullptr) {
Destroy();
return;
}

// Get the instigator controller of the owner
AController* MyOwnerInstgiator = MyOwner->GetInstigatorController();

// Check if the other actor is not the owner before applying damage
if (OtherActor && OtherActor != this && OtherActor != MyOwner) {
// Apply direct damage to the other actor
UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstgiator, this, UDamageType::StaticClass());

// Spawn hit particles at the location of the hit
UGameplayStatics::SpawnEmitterAtLocation(this, HitParticles, GetActorLocation(), GetActorRotation());

// Apply AOE radial damage around the hit location
UGameplayStatics::ApplyRadialDamage(this, Damage, GetActorLocation(), AoeRange, UDamageType::StaticClass(), TArray<AActor*>(), this, MyOwnerInstgiator);
}

// Play hit sound, shake camera and destroy the projectile
if (HitSound) {
UGameplayStatics::PlaySoundAtLocation(this, HitSound, GetActorLocation(), GetActorRotation());
}
if (HitCameraShakeClass) {
GetWorld()->GetFirstPlayerController()->ClientStartCameraShake(HitCameraShakeClass);
}
Destroy();
}

This code is from the OnHit() function of the AProjectile class. It’s important to note that this code is just an example and you may need to adjust it to your own specific project.

In this example, the OnHit function is called when the projectile collides with another object. First, we get the owner of the projectile and the instigator controller of the owner. Then, we check if the other actor is not the owner before applying damage. If the other actor is valid, we proceed to apply damage.

We apply direct damage to the other actor by calling the ApplyDamage() function on the OtherActor. We also spawn hit particles at the location of the hit by calling the SpawnEmitterAtLocation() function. Then, we apply AOE radial damage around the hit location by calling the ApplyRadialDamage() function.

After applying damage, we play a hit sound, shake the camera, and finally, we destroy the projectile.

This example shows how to create AOE damage in a C++ class in Unreal Engine using the ApplyDamage and ApplyRadialDamage functions of the UGameplayStatics class, SpawnEmitterAtLocation and PlaySoundAtLocation, and ClientStartCameraShake function.

The example source code provided is a C++ class called “AProjectile.” This class is responsible for handling the logic when a projectile collides with another object. The OnHit() function is where the AOE damage is being applied.

The first step in creating AOE damage is to get the owner of the projectile. This is done by calling the GetOwner() function. This function returns a pointer to the AActor that owns the projectile.

The next step is to get the instigator controller of the owner. This is done by calling the GetInstigatorController() function on the owner. The instigator controller is the controller that is responsible for creating the projectile.

After getting the instigator controller, we check if the other actor is not the owner before applying damage. This is done by using an if statement that checks if OtherActor is not null, is not the same as the projectile, and is not the same as the owner. If these conditions are met, we can proceed with applying damage.

The first type of damage that is being applied is direct damage. This is done by calling the ApplyDamage() function on the OtherActor. The ApplyDamage() function takes in several parameters such as the amount of damage to apply, the instigator controller, and the damage type.

The next type of damage that is being applied is radial damage. This is done by calling the ApplyRadialDamage() function. The ApplyRadialDamage() function takes in several parameters such as the amount of damage to apply, the location of the damage, the range of the damage, and the damage type.

After applying the damage, we spawn an emitter at the location of the hit, play a hit sound, shake the camera, and finally, destroy the projectile.

In conclusion, creating AOE damage in a C++ class in Unreal Engine is a relatively straightforward process. By using the example source code provided, developers can easily create an AOE damage system in their own projects. It’s important to note that the example provided is just one way to create AOE damage, and developers can improve and customize it based on their needs.

--

--

Rambod
Rambod

Written by Rambod

I‘m a full-stack developer with a knack for making games and apps. I have been developing for over 10 years and have a deep understanding of the Unreal engine

No responses yet