I have a problem getting collisions of components to work in C++.
My goal is a subclass of the UStaticMeshComponent that checks for collision with other Actor’s Collision components.
I have the following constructor:
UWeaponComponent::UWeaponComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMeshOb_torus(TEXT("StaticMesh'/Game/TopDown/Meshes/CubeMesh.CubeMesh'"));
if (StaticMeshOb_torus.Object)
SetStaticMesh(StaticMeshOb_torus.Object);
bGenerateOverlapEvents = true;
OnComponentBeginOverlap.AddDynamic(this, &UWeaponComponent::OnBeginOverlap);
SetCollisionResponseToChannel(ECollisionChannel::ECC_GameTraceChannel1, ECollisionResponse::ECR_Overlap);
SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
SetCollisionObjectType(ECC_GameTraceChannel1);
}
and the following OnBeginOverlap method:
void UWeaponComponent::OnBeginOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool x, const FHitResult& hit)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, "Hit !!!");
}
My problem is: the OnBeginOverlap method is never executed. I don’t think there is a problem with the collision channels because I can simply add the OnComponentBeginOverlap event for this component in the blueprint and it works without any any changes in the collision settings.
Here is my collision component constructor (for completeness):
UHitDetectionComponent::UHitDetectionComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
OnComponentBeginOverlap.AddDynamic(this, &UHitDetectionComponent::OnBeginOverlap);
SetCollisionResponseToChannel(ECollisionChannel::ECC_GameTraceChannel1, ECollisionResponse::ECR_Overlap);
bGenerateOverlapEvents = true;
SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
SetCollisionObjectType(ECC_GameTraceChannel1);
}
Am I missing something?
I am even more confused because the HitDetectionComponents work as intended. If two characters with these components collide the corresponding C++ method OnBeginOverlap is called.