I am trying to decrease the number of lives a player has when a ball hits an actor marked with the “deadzone” tag but sometimes, when the ball hits the deadzone, 2,3 or 4 lives are removed and I do not know why. Here is my OnHit code:
void ABall::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
// Other Actor is the actor that triggered the event. Check that is not ourself.
if ((OtherActor != nullptr) && (OtherActor != this) && (OtherComponent != nullptr))
{
if (OtherActor->ActorHasTag("DeadZone") /*&& !bHasCollided*/)
{
//UE_LOG(LogTemp, Warning, TEXT("Dead"));
BallMeshComponent->SetRelativeLocation(StartingLocation);
BallMeshComponent->SetSimulatePhysics(false);
bIsLaunched = false;
CurrentNumberOfLives--;
//bHasCollided = true; //I can prevent multiple hits with a boolean but would rather understand why it's happening
}
}
}
When I use a breakpoint to debug, I can see that the function is called multiple times, but I really don’t know why. Sometimes it works fine, but most of the time, it’s triggered on multiple occasions. Any help appreciated.