I am using the punching montage from the tutorial to use in a short game. I attached sphere components to the hand of enemy characters and they start the montage when they are near my player character. I than used the OnComponetBeginOverlap.AddDynamic on both the sphere on each hand. the function that is called by it collects all the overlapped actors, and if one of the actor is player it reduces player health. the problem is that instead of triggering once each pump, it is triggering multiple times and the player dies in 1-2 punches. how i solve the problem. can someone please help fast.
This is the oncomponentbeginoverlap
void AAI_Character::SetIsPunching()
{
IsPunching = false;
LeftCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
RightCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
//Get overlapping actors and store it in an array
TArray<AActor *> CollectedActors;
CollectionSphere->GetOverlappingActors(CollectedActors);
//for each collected actors
for (int32 iCollected = 0; iCollected < CollectedActors.Num(); iCollected++)
{
//cast the actor to player character
AMyProjectCharacter * const MyCharacter = Cast<AMyProjectCharacter>(CollectedActors[iCollected]);
if (MyCharacter)
{
IsPunching = true;
LeftCollision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
RightCollision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
LeftCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::DamageLeft);
RightCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::DamageRight);
}
}
}
these are the functions called
void AAI_Character::DamageLeft(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
UE_LOG(LogClass, Log, TEXT("Left punch"));
TArray<AActor *> actors;
LeftCollision->GetOverlappingActors(actors);
for (int32 iCollected = 0; iCollected < actors.Num(); iCollected++)
{
AMyProjectCharacter * MyCharacter = Cast<AMyProjectCharacter>(actors[iCollected]);
if (MyCharacter)
{
MyCharacter->UpdateHealth(-1.f);
}
}
}
void AAI_Character::DamageRight(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
UE_LOG(LogClass, Log, TEXT("Right Punch"));
TArray<AActor *> actors;
RightCollision->GetOverlappingActors(actors);
for (int32 iCollected = 0; iCollected < actors.Num(); iCollected++)
{
AMyProjectCharacter * MyCharacter = Cast<AMyProjectCharacter>(actors[iCollected]);
if (MyCharacter)
{
MyCharacter->UpdateHealth(-1.f);
}
}
}