Hello, I’m still a newbie so I don’t know if there’s a better method to do this.
SweepMultiByChannel fills the TArray with all the components it intercepts, but I’m using it to generate a grenade explosion, so I want to damage each actor inside its range only once.
In order to achieve this I wrote the following code:
TArray<FHitResult> ComponentsInExplosionRange;
GetWorld()->SweepMultiByChannel(
ComponentsInExplosionRange,
HitComponent->GetComponentLocation(),
HitComponent->GetComponentLocation(),
FQuat::Identity, ECC_Pawn,
FCollisionShape::MakeSphere(300.f)
);
TSet<APawnBase*> HitPawns;
for (const auto& Component : ComponentsInExplosionRange)
{
const auto ActorHit = Cast<APawnBase>(Component.GetActor());
if (ActorHit)
{
HitPawns.Add(ActorHit, nullptr);
}
}
for (const auto& Pawn : HitPawns)
{
UGameplayStatics::ApplyDamage(
Pawn,
Damage,
ProjectileOwner->GetInstigatorController(),
this,
DamageType
);
}
This is working, but is it a good approach to the problem? Or are there more efficient/elegant ways to obtain the same results?
Thank you!