Collision fails to trigger C++ UE5

I’ve followed a couple of tutorials on creating custom collision events, but mine never triggers. I’m getting desperate! I’ve almost literally copied an existing working UE4 project, but cannot get the collision event to fire. Please help!

My header:

#pragma once

#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"
#include "StarWarsDrone.generated.h"

UCLASS()
class STARWARSTRAINING_UE5_API AStarWarsDrone : public AActor
{
	GENERATED_BODY()
	
	UPROPERTY(EditAnywhere)
		AActor* Enemy;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision, meta = (AllowPrivateAccess = "true"))
		class UBoxComponent* CollisionBox;


public:	
	// Sets default values for this actor's properties
	AStarWarsDrone();

	UFUNCTION()
	void OnAttackHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

my .cpp:

#include "StarWarsDrone.h"

AStarWarsDrone::AStarWarsDrone()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
	CollisionBox->SetupAttachment(RootComponent);
	CollisionBox->SetCollisionProfileName("NoCollision");
	CollisionBox->SetNotifyRigidBodyCollision(false);
	CollisionBox->SetHiddenInGame(false);

}

void AStarWarsDrone::BeginPlay()
{
	Super::BeginPlay();

	// attach collision components to sockets based on transformations definitions
	const FAttachmentTransformRules AttachmentRules(EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, EAttachmentRule::KeepWorld, false);

	CollisionBox->AttachToComponent(RootComponent, AttachmentRules, "SphereCollisionSocket");
	CollisionBox->OnComponentHit.AddDynamic(this, &AStarWarsDrone::OnAttackHit);

}

void AStarWarsDrone::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AStarWarsDrone::OnAttackHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	UE_LOG(LogTemp, Warning, TEXT("OUCH"));
}

In editor, CollisionBox has “simulation generates hit events” set to True.
Any tips as to why this never triggers? Thanks!

You are setting the “simulation generates hit events” on the wrong object. You need to set this on the object that is hitting the StarWarsDrone not on the drone itself.

You cannot use CollisionBox->SetCollisionProfileName(“NoCollision”);

This makes the the hit response not work. You need to set it to blockAll.
If you want things to pass through the box then you need to switch to overlap in place of hit

change
CollisionBox->SetCollisionProfileName(“NoCollision”);
to
CollisionBox->SetCollisionProfileName(“BlockAll”);

If you want overlap in c++

.h

	UFUNCTION()
		void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

.cpp


void AStarWarsDrone::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("Activated"));

}

in contructor swap out the preset to OverlapAll

CollisionBox->SetCollisionProfileName("OverlapAll");

Thanks! I tried those last night, with both Overlap and Hit events, still nothing. I’ll tidy it up and post my current code later on… am wondering if there is something odd about how I’ve made the drone asset. Its RootComponent is just a StaticMesh ( a sphere), which has a socket called SphereCollisionSocket. The sphere is much smaller than the CollisionBox, so I’m assuming that whatever collision properties the sphere has, won’t affect the collisions I’m trying to detect. But I shall post again later :slight_smile:
thanks!

Recheck the collision settings in the actor in the editor. It may keep the old “nocollision” profile.
You just changed the default value in the constructor so you may have an arrow next to it to revert to default (it will switch to the constructor value then)