How do I detect what actor my projectile is colliding with and deal damage?

Hi Lewis,

There are different approaches depending on whether you want to accomplish this by code or with Blueprints. In either case, I recommend looking at our sample projects like ShooterGame, or creating a new project based on the First Person Shooter templates (either code or Blueprint).

If you go the Blueprint route, then you will want to take a look at the MyProjectile Blueprint. Note the OnComponentBeginOverlap node, and instead of Add Impulse you can use an Apply Damage.

If you choose to create a code template instead, then you can see an example in codeProjectile.h where it calls OnOverlap and codeProjectile.cpp where it adds an impulse (and just like in Blueprints, you can substitute with damage instead). An example of projectile damage can be found in the ShooterGame example.

Hopefully this helps start you out.

Cheers!

Thanks for replying and sorry I made the question very vague. I’m doing it in c++ and I’m using shooter template how would I go about having damage applied if it hit’s a certain actor. Tnx

There is no simple and concise answer I can give for how best to accomplish this by code, and we do not generally provide support for how to write the code for gameplay.

I recommend looking at the code for ShooterGame (the provided sample which can be downloaded) for an example of how to accomplish damaging an actor in C++. Specifically you will want to take a look at ShooterWeapon_Instant.cpp, and some of the functions such as ProcessInstantHit and DealDamage.

Cheers

Dear Lewis,

What I did was copy over all the projectile class code to make my own projectile class

There is a StopDelegate that you can set on the movement component to track what actor the projectile hit that stopped its movement

There is also a bounce delegate if you enable bouncing for the projectile

Below I show you how to obtain the HitActor, which you can then apply damage to using your chosen method.

.h

/** handle hit */
void OnImpact(FHitResult const & HitResult);

.cpp

void AVictoryProjectile::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	//Stop Delegate!
	MovementComp->SetStopDelegate(FOnImpactDelegate::CreateUObject(this, &AVictoryProjectile::OnImpact));
}	
	
//Projectile Stop Delegate
void AVictoryProjectile::OnImpact(FHitResult const& HitResult)
{
	//detecting the actor that was hit
	AActor * HitActor = HitResult.GetActor();
	
	if (Role == ROLE_Authority && !bExploded)
	{
		Explode(HitResult);
		DisableAndDestroy();
	}
}

#The Damage

One option is in GameplayStatics.h

to use gameplaystatics functions you need to have this include in your .h file

#include "EngineKismetLibraryClasses.h"

:slight_smile:

UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category="Game")
00171: static void ApplyDamage(AActor* DamagedActor, float BaseDamage, AController* EventInstigator, AActor* DamageCauser, TSubclassOf DamageTypeClass);
00172:

#Final

//Projectile Stop Delegate
    void AVictoryProjectile::OnImpact(FHitResult const& HitResult)
    {
	//detecting the actor that was hit
	AActor * HitActor = HitResult.GetActor();

	if(HitActor) UGameplayStatics::ApplyDamage(HitActor....);

	if (Role == ROLE_Authority && !bExploded)
	{
		Explode(HitResult);
		DisableAndDestroy();
	}
}