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