# OnHit not working correctly C++

**URL:** <https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 11, 2015, 4:20pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666 "2015-02-11T16:20:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crabbo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crabbo](https://forums.unrealengine.com/u/Crabbo)\
**Post date:** [February 11, 2015, 4:20pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666/1 "2015-02-11T16:20:07Z")

</div>

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. The contents of the OnHit is to just destroy the actor.  
After looking through all the different collision presets I used the blueprint OnHit nad made my OnHit blueprint callable:

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/c/1/8c121945024168fb9bfb4589ae670e94bd0d8681.png) ](https://i.imgur.com/cl0tsD2.png)

This made it work. So now I am confused as to why just using C++, the OnHit isn’t called?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [February 12, 2015, 3:11pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666/2 "2015-02-12T15:11:44Z")

</div>

Hi Crabbo,

Event Hit and Component Hit it is different functions. I think collision properties can be reason of different behavior of these functions. Try to this function:

```
OnActorHit.AddDynamic(this, &ABaseProjectile::OnHitActor);

```

Hope it helps!

---

<div class="post-metadata">

**Author:** ![Crabbo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crabbo](https://forums.unrealengine.com/u/Crabbo)\
**Post date:** [February 12, 2015, 4:37pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666/3 "2015-02-12T16:37:37Z")

</div>

I get where this is coming from but I have ran into this problem:

```
[BaseProjectile.h]
USphereComponent* BaseCollisionComponent;
[]

[BaseProjectile.cpp]

BaseCollisionComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseSphereComponent"));
	BaseCollisionComponent->InitSphereRadius(5.0f);
	BaseCollisionComponent->BodyInstance.SetCollisionProfileName("Projectile");
	BaseCollisionComponent->OnActorHit.AddDynamic(this, &ABaseProjectile::OnHit);

	//Set sphere to be root component
	RootComponent = BaseCollisionComponent;

```

My problem is that the sphere component does not have an OnActorHit() function. Is there any other functions I should be looking at to use to get the collisions correct?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [February 12, 2015, 4:43pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666/4 "2015-02-12T16:43:10Z")

</div>

OnActorHit it is actor delegate, not component.

Also, try to use it

BaseCollisionComponent-\>SetCollisionEnabled(ECollisionEnabled::QueryOnly);

BaseCollisionComponent -\>SetCollisionResponseToAllChannels(ECR\_Block);

---

<div class="post-metadata">

**Author:** ![Crabbo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crabbo](https://forums.unrealengine.com/u/Crabbo)\
**Post date:** [February 13, 2015, 11:27pm UTC](https://forums.unrealengine.com/t/onhit-not-working-correctly-c/306666/5 "2015-02-13T23:27:22Z")

</div>

Thank you for the help. To fix it I used:

```
this->OnActorHit.AddDynamic(this, &ABaseProjectile::OnHit);

```

But I had to change my OnHit attributes from:

```
ABaseProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)

```

To:

```
ABaseProjectile::OnHit(AActor* SelfActor, AActor* OtherActor, FVector NormalImpulse, const FHitResult& Hit)

```

Thanks again for the help.
