Character Collision

There are two ways of detecting collision for an actor. Either you use the built in Hit function or you bind a function to a UPrimitiveComponent’s OnComponentHit multicast delegate.

Approach #1:
Override

virtual void NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit);

and implement your logic there. Don’t forget to call Super::NotifyHit!

Approach #2:

Say you have a reference to a specific primitive component that supports collision like a static mesh or a capsule collider. You can add a custom UFUNCTION to its OnComponentHit delegate like this:

UFUNCTION()
void YourCustomOnHitFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
    
    
        
void AYourActor::BeginPlay()
{
    Super::BeginPlay();
    YourPrimitiveComponent->OnComponentHit.AddDynamic(this, &AYourActor::YourCustomOnHitFunction);
        }
    
void YourCustomOnHitFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
      //Your logic here
}

Don’t forget to remove your function from the delegate for example in EndPlay by calling RemoveDynamic instead of AddDynamic. It uses the same signature.

Sorry for the weird layout but the code sample block is not that useful in terms of easy to read layout.