Difficulty getting OnComponentHit method to fire - C++

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?

Does anyone have a solution for this problem?

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.

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

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.

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

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.

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.

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.

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

Hi Sean,

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

Hi Sean,

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

Thank you

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.

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.

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

I believe I am doing this here:

86993-collisionerror.jpg

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

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