I am using the OnComponentBeginOverlap to detect if the punches of my character are hitting other. I used this code for the player character for the function called by OnComponentBeginOverlap:
void AMyProjectCharacter::Damage(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
AAI_Character * Enemy = Cast<AAI_Character>(OtherActor);
if (Enemy)
{
if(OtherComp == Enemy->GetCapsuleComponent())
{
UE_LOG(LogClass, Log, TEXT("Player punched"));
Enemy->UpdateHealth(-10.f);
}
}
}
It is working fine, the Player punch is logged only when the punch hits the enemy and after 5 punches the enemy is destroyed as my enemy health is 50. However i used the same for my enemy character:
void AAI_Character::Damage(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
AMyProjectCharacter * MyCharacter = Cast<AMyProjectCharacter>(OtherActor);
if (MyCharacter)
{
if (OtherComp == MyCharacter->GetCapsuleComponent())
{
UE_LOG(LogClass, Log, TEXT("Enemy punched"));
//MyCharacter->UpdateHealth(-10.f);
}
}
}
But with each punch of the enemy, the function gets called many times(3-5 times). I dont get why is it happening. Both the capsule components are identical, For both, OnComponentBeginOverlap is called inside BeginPlay but still this is happenening. The OnComponentBeginOverlap are as follows:
void AMyProjectCharacter::BeginPlay()
{
Super::BeginPlay();
LeftCollision->OnComponentBeginOverlap.AddDynamic(this, &AMyProjectCharacter::Damage);
RightCollision->OnComponentBeginOverlap.AddDynamic(this, &AMyProjectCharacter::Damage);
}
and
void AAI_Character::BeginPlay()
{
Super::BeginPlay();
LeftCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::Damage);
RightCollision->OnComponentBeginOverlap.AddDynamic(this, &AAI_Character::Damage);
}
can anyone please help me with my problem. I am new to ue and I found that the answerhub is quit unresponsive to questions. so please someone help me with this.