# How to get onhit events to fire with collisions?

**URL:** <https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [October 14, 2015, 6:12pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685 "2015-10-14T18:12:09Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Jerry.Richards](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jerry.Richards](https://forums.unrealengine.com/u/Jerry.Richards)\
**Post date:** [October 27, 2015, 2:24pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685/4 "2015-10-27T14:24:50Z")

</div>

I’m not sure if you got this fixed or not but for me it is a little misleading. I prefer that any mesh, sound, collision components to be set in the BP without any requirements for the C++ side of things. After all that’s the beauty of the integration between C++ and BP. And I’ve run into this exact problem.

Have you tried:

**For the bullet:**

```auto

.h
    UFUNCTION()
    void OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit);

    UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Movement)
    UProjectileMovementComponent* ProjectileMovement;

.cpp
AMyBullet::AMyBullet(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    ProjectileMovement = ObjectInitializer.CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("Movement"));
    ProjectileMovement->UpdatedComponent = RootComponent;

    OnActorHit.AddDynamic(this, &AMyBullet::OnHit);

    InitialLifeSpan = 30.0f;
    PrimaryActorTick.bCanEverTick = true;
}

void AMyBullet::SetInitVelocity(const FVector& ShootDirection)
{
    if (ProjectileMovement)
    {
        ProjectileMovement->Velocity = ShootDirection * ProjectileMovement->InitialSpeed;
    }
}

void AMyBullet::OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
{
    FColor DisplayColor = FColor::Yellow;
    const FString DebugMessage(OtherActor->GetName());
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, DisplayColor, DebugMessage);

    if (OtherActor)
    {
        if (OtherActor->IsA(APawn::StaticClass()))
        {
            APawn* Pawn(Cast<APawn>(OtherActor));
            if (Pawn)
            {
                float DamageAmount = 600.0f;
                const FDamageEvent DamageEvent;
                AController* EventInstigator = nullptr;
                AActor* DamageCauser = this;

                Pawn->TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
            }
        }
    }

    Destroy();
}

```

**For an actor/pawn:**

```auto

.h
    UFUNCTION()
    void OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit);

.cpp
AMyPawn::AMyPawn(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    PrimaryActorTick.bCanEverTick = true;
    OnActorHit.AddDynamic(this, &AMyPawn::OnHit);
}

void AMyPawn::OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)
{
    FColor DisplayColor = FColor::Yellow;
    const FString DebugMessage(OtherActor->GetName());
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, DisplayColor, DebugMessage);

    if (OtherActor)
    {
        if (OtherActor->IsA(ABullet::StaticClass()))
            Destroy();
    }
}

```

This is still very much a work-in-progress that I am sharing incase it helps in some way.

The little OnActorHit.AddDynamic() was found here:

> **[OnHit not working correctly C++](https://forums.unrealengine.com/questions/171631/onhit-not-working-correctly-c.html)**
>
> I have tried to create an OnHit function and I have attached it to my base sphere component: BaseCollisionComponent-\>OnComponentHit.AddDynamic(this, &ABaseProjectile::OnHit); Whenever my projectile actor touched anything this never fired....

And a really nice projectile walkthrough is here:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

Btw, while running the game press ~, then enter “show collision” to see the collision boxes.

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685)._
