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!