# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_31d9bc6e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_31d9bc6e](https://forums.unrealengine.com/u/anonymous_user_31d9bc6e)\
**Post date:** [October 14, 2015, 6:12pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685/1 "2015-10-14T18:12:09Z")

</div>

I’ve been stuck on this problem for the last 2 days and I can’t seem to find any useful documentation.  
All I want is for the onhit() function to fire when an object is hit.

In constructor of .cpp file

```auto

Bullet = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Bullet"));

static ConstructorHelpers::FObjectFinder<UStaticMesh> MyBullet(TEXT("/Game/StarterContent/Shapes/Shape_Sphere"));

Bullet->SetStaticMesh(MyBullet.Object);

Bullet->BodyInstance.SetCollisionProfileName("Projectile");

Bullet->SetSimulatePhysics(true);

Bullet->OnComponentHit.AddDynamic(this, &ABullet::OnHit);

void ABullet::OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{

	Destroy();
	if (OtherActor && (OtherActor != this) && OtherComp)
	{
		OtherComp->DestroyComponent(false);

	}

```

I’ve tried looking at samples, tutorials, it just never seems to work

---

<div class="post-metadata">

**Author:** ![Pumpy\_Bird](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pumpy\_Bird](https://forums.unrealengine.com/u/Pumpy_Bird)\
**Post date:** [October 14, 2015, 7:46pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685/2 "2015-10-14T19:46:42Z")

</div>

Make sure both actors have simulation generated hit events enabled.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_31d9bc6e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_31d9bc6e](https://forums.unrealengine.com/u/anonymous_user_31d9bc6e)\
**Post date:** [October 14, 2015, 8:49pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685/3 "2015-10-14T20:49:07Z")

</div>

Thank you so much Pumpy you’ve really helped me out :).  
Here’s the code for setting up the generate hit events within a class in case anyone wondered.

Bullet-\>SetNotifyRigidBodyCollision(true);

---

<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.

---

<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, 7:58pm UTC](https://forums.unrealengine.com/t/how-to-get-onhit-events-to-fire-with-collisions/44685/5 "2015-10-27T19:58:10Z")

</div>

Another update:

Instead of relying on C++ code to know anything about collisions, other than responding to the OnHit event, I was able to set the collision component on my **static mesh** projectile with the following:

```auto

Collision Complexity: DEFAULT
Customized Collision: CHECKED

```

And in the **Blueprint Class** that includes the static mesh I have the following:

```auto

Simulate Physics: UNCHECKED

Collision
Simulation Generates Hit Events: CHECKED
Collision Presets: PAWN
Use CCD: CHECKED

```

Now my projectiles and (mechanical) skeletal meshes collide properly.

Btw, the ProjectileMovement component has a Projectile Gravity Scale, which defaults to 1, giving your projectiles the parabolic trajectory.
