I have made an Actor class to detect collision, i’m sure this is an easy fix.
However, if I place a sphere or any other shape inside my triggerbox, the OnTriggerEnter and OnTriggerExit events never get called - the debug messages never appear ingame.
TriggerDetection.cpp :
// Sets default values
ATriggerDetection::ATriggerDetection(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryActorTick.bCanEverTick = true;
FVector* Stretch = new FVector(3.0f,3.0f,3.0f);
Box = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Box"));
Box->bGenerateOverlapEvents = true;
Box->SetRelativeScale3D(*Stretch);
RootComponent = Box;
Box->OnComponentBeginOverlap.AddDynamic(this, &ATriggerDetection::TriggerEnter);
Box->OnComponentEndOverlap.AddDynamic(this, &ATriggerDetection::TriggerExit);
}
// Called when the game starts or when spawned
void ATriggerDetection::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATriggerDetection::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void ATriggerDetection::TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
GEngine->AddOnScreenDebugMessage(0, 1.0f, FColor::White, TEXT("Something is inside trigger"));
}
void ATriggerDetection::TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
GEngine->AddOnScreenDebugMessage(0, 1.0f, FColor::White, TEXT("Something has exited trigger"));
}
TriggerDetection.h:
UCLASS()
class CPPBOXTRIGGER_API ATriggerDetection : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATriggerDetection(const FObjectInitializer& ObjectInitializer);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UShapeComponent* Box;
UFUNCTION()
void TriggerEnter(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void TriggerExit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
Help Would be appreciated, thanks!