# Difficulty getting OnComponentHit method to fire - C++

**URL:** <https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133>\
**Category:** Pipeline & Plugins\
**Tags:** oncomponenthit, collision-detection, unreal-engine, Visual-Studio, UE4, question, CPP\
**Created:** [March 20, 2016, 1:27pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133 "2016-03-20T13:27:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [March 20, 2016, 1:27pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/1 "2016-03-20T13:27:10Z")

</div>

I am having some difficulty getting my collision event to fire. I create a collision component dynamically in the constructor of the a class and set it as the root component. I then attach a static mesh to the root component. This is my code:

```
CollisionComp = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("CollComp"));
CollisionComp->OnComponentHit.AddDynamic(this, &ABoid::OnHit);

CollisionComp->BodyInstance.SetCollisionProfileName("boid");
CollisionComp->BodyInstance.SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics, true);

CollisionComp->SetCollisionObjectType(COLLISION_BOID);
CollisionComp->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
CollisionComp->SetCollisionResponseToChannel(COLLISION_BOID, ECollisionResponse::ECR_Ignore);

CollisionComp->BodyInstance.SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics, true);
CollisionComp->SetNotifyRigidBodyCollision(true);

RootComponent = CollisionComp;

mesh = CreateAbstractDefaultSubobject<UStaticMeshComponent>(TEXT("BoidMesh"));

static ConstructorHelpers::FObjectFinder <UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_QuadPyramid.Shape_QuadPyramid'"));
static ConstructorHelpers::FObjectFinder <UMaterial> Material(TEXT("Material'/Game/StarterContent/Materials/M_AssetPlatform.M_AssetPlatform'"));

mesh->SetStaticMesh(StaticMesh.Object);
mesh->SetMaterial(0, Material.Object);
mesh->SetWorldScale3D(FVector(0.7f, 0.6f, 0.6f));
mesh->SetNotifyRigidBodyCollision(true);

mesh->AttachTo(RootComponent);

```

The problem I get is that the collision does not fire for every collision. I have a flying pawn with the player controls. When the component collides with this flying pawn, a collision occurs. However, the component does not collide with any other static mesh (blocks, etc.) so they just go through them. My OnHit function code looks like this:

```
UE_LOG(LogTemp, Log, TEXT("Should have collided!"));
health--;
if (health <= 0)
{
	Destroy();
	UE_LOG(LogTemp, Log, TEXT("Boid died"));
}

```

I can successfully destroy a Boid with the flying pawn but they do not get destroyed upon collision with a StaticMeshActor such as a cube.

Do I need to set up some code for the objects I am colliding with? If so, what is this?

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [March 27, 2016, 4:15pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/2 "2016-03-27T16:15:44Z")

</div>

Does anyone have a solution for this problem?

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 1, 2016, 11:43pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/3 "2016-04-01T23:43:28Z")

</div>

Is there any way that I can make the UE staff more aware of this? Seems like it should just be a simple solution that I’m not getting.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 6, 2016, 2:08pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/4 "2016-04-06T14:08:35Z")

</div>

Hello,

From what you’ve described, it seems like you will need to go into the settings of your static meshes that are not colliding with the pawn and ensure that they are set to Block All collision and they have Generate Hit Events enabled.

Let me know if this helps, or else we can look into some other solutions.

Have a great day

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 6, 2016, 2:51pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/5 "2016-04-06T14:51:34Z")

</div>

Hi Sean,

Thank you for your response. I have already done this with the static meshes and it hasn’t done anything. The UFO pawn collides with all static meshes fine. But my dynamically spawned actors don’t.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 7, 2016, 3:34pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/6 "2016-04-07T15:34:56Z")

</div>

Are you setting the collision preset for those dynamically spawned actors in your code?

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 7, 2016, 4:04pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/7 "2016-04-07T16:04:55Z")

</div>

The code in my question is what I am doing in my dynamically spawned actors (in the constructor). I’m not sure exactly what a collision preset is, but I did follow a tutorial to set up the collision component in the constructor as I have done.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 7, 2016, 4:08pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/8 "2016-04-07T16:08:36Z")

</div>

I see that you are setting a collision preset:  
" CollisionComp -\>SetCollisionResponseToAllChannels(ECollisionResponse::ECR\_Block);"

Are you ever setting GenerateHitEvents to true on your cube mesh? You’ll need both colliding objects to have this enabled as well if you are using hit events to destroy your player.

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 7, 2016, 4:17pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/9 "2016-04-07T16:17:29Z")

</div>

I have set this on all my static meshes on the map. I believe the line:  
mesh-\>SetNotifyRigidBodyCollision(true);

is used to set GenerateHitEvents to true.

I have a player controlled pawn which DOES collide with these actors, therefore I am fairly certain that my code is correct. I just can’t get it to collide which the static meshes on the map.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 11, 2016, 2:02pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/10 "2016-04-11T14:02:41Z")

</div>

Would it be possible to provide a zipped up copy of your project? I’d like to take a look at your setup and determine if there is a bug involved, or if there is just a setting that is missing.

If you zip it up and upload it to dropbox, you can provide me with a link through PM on the forums at this link: [https://forums.unrealengine.com/member.php?160394-Sean-Flint](https://forums.unrealengine.com/member.php?160394-Sean-Flint)

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 11, 2016, 4:28pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/11 "2016-04-11T16:28:13Z")

</div>

Hi Sean,

I have just sent you a private message with the dropbox in it.

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 14, 2016, 11:46am UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/12 "2016-04-14T11:46:55Z")

</div>

Hi Sean,

Have you had a chance to look at my project yet?

Thank you

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 14, 2016, 7:37pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/13 "2016-04-14T19:37:29Z")

</div>

Hey CosmosD,

I have actually looked at the project today. What you can try is to go into your TestBoidsPawn.cpp class and add this line of code under your RootComponent = PlaneMesh; line.

```
PlaneMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);

```

This will need to be altered to only set the response to the object channel of your boid since you created a custom channel for it. Go ahead and give this a try and see if it works for you.

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 14, 2016, 8:18pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/14 "2016-04-14T20:18:29Z")

</div>

Hi Sean,

Thank you for looking into this.

The pawn and boids already collide with each other, it’s just that the boids don’t collide with any of the other static meshes on the map. Therefore I’m not sure if that will achieve anything as it isn’t an issue with the pawn class.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 14, 2016, 8:22pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/15 "2016-04-14T20:22:17Z")

</div>

Ah, okay. I misinterpreted your issue. My mistake.

Have you ensured that the other meshes on the map are set to block that specific object type? (The type that you have created for the boids).

---

<div class="post-metadata">

**Author:** ![CosmosD](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CosmosD](https://forums.unrealengine.com/u/CosmosD)\
**Post date:** [April 15, 2016, 11:26am UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/16 "2016-04-15T11:26:35Z")

</div>

I believe I am doing this here:

![86993-collisionerror.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/7/f/07f6cf96230e3baf72d3d100d15a3d0587660a07.jpeg)

I do have a workaround where I raycast so that the boids avoid the obstacles so they don’t have to collide with them.

---

<div class="post-metadata">

**Author:** ![Sean\_Flint](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sean\_Flint](https://forums.unrealengine.com/u/Sean_Flint)\
**Post date:** [April 25, 2016, 1:48pm UTC](https://forums.unrealengine.com/t/difficulty-getting-oncomponenthit-method-to-fire-c/351133/17 "2016-04-25T13:48:47Z")

</div>

Hello,

I apologize for the delay. After investigating this issue, it appears that there is a potential issue with OnComponentHit in code. We are currently performing additional testing, but in the meantime you can either try using OnActorHit instead, or set up OnComponentHit through blueprint, as it seems to be working there.

Thank you for your report.

Have a great day
