I have a class derived from a character. I’m trying to add some functionality when a hit event occurs.
In my .h I have added (under public):
UFUNCTION()
void OnHit(AActor* other, class UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit);
And in the constructor of my .cpp I have added:
GetCapsuleComponent()->OnComponentHit.AddDynamic(this, &AMainCharacter::OnHit);
Lastly, I have defined the function in my .cpp
void AMainCharacter::OnHit(AActor* other, class UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit)
{
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Hit.");
}
I’ve researched numerous topics, and, as far as I can tell this is the correct code. However, I do not get a printed statement when a hit occurs. The collider in the blueprint is definitely set to generate hit events, yet still I get nothing. If I implement the functionality in Blueprint, it works. So, I don’t think it’s a problem with collision channels or anything else. I’ve also tried adding another collision component to test if I could get that one to work. I could not. As a matter of fact, I had no access to the “Simulation Generates Hits” bool within Blueprint with that collider, so, I couldn’t even get that to generate hits in Blueprint! Any ideas about what might be going wrong?