Hi,
From my experience, you should write these lines in the constructor as well:
EnemyTriggerVolume->SetSphereRadius(EnemyTriggerRadius);
EnemyTriggerVolume->OnComponentBeginOverlap.AddDynamic(this, &APlayerCharacter::OnOverlapBegin);
EnemyTriggerVolume->OnComponentEndOverlap.AddDynamic(this, &APlayerCharacter::OnOverlapEnd);
Here is some example code from my side project where I use a sphere component and collision:
.h
protected:
UPROPERTY(VisibleAnywhere)
class USphereComponent* ItemCollision;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class USphereComponent* ItemDetectionSphere;
protected:
UFUNCTION()
void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
UFUNCTION()
void OnDetectionBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
const FHitResult& SweepResult);
.cpp
#include "Hero/Item/Collectible/HeroItemCollectible.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "GameFramework/RotatingMovementComponent.h"
#include "Hero/PlayerCharacter/HeroPlayerCharacter.h"
#include "Kismet/KismetMathLibrary.h"
#include "Components/TimelineComponent.h"
#include "Runtime/Engine/Classes/Components/SphereComponent.h"
AHeroItemCollectible::AHeroItemCollectible()
{
SetActorTickEnabled(true);
PrimaryActorTick.bCanEverTick = true;
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
RotatingMovement = CreateDefaultSubobject<URotatingMovementComponent>(TEXT("RotatingMovement"));
ItemCollision = CreateDefaultSubobject<USphereComponent>(TEXT("ItemCollision"));
ItemDetectionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("ItemDetectionSphere"));
ItemCollision->SetupAttachment(ItemMesh);
ItemDetectionSphere->SetupAttachment(ItemMesh);
//No Collision by default for Item Detection
ItemDetectionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
ItemCollision->OnComponentBeginOverlap.AddDynamic(this, &AHeroItemCollectible::OnOverlapBegin);
ItemDetectionSphere->OnComponentBeginOverlap.AddDynamic(this, &AHeroItemCollectible::OnDetectionBegin);
}
void AHeroItemCollectible::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
AHeroPlayerCharacter* Player = Cast<class AHeroPlayerCharacter>(OtherActor);
if (Player)
{
Destroy();
}
}
void AHeroItemCollectible::OnDetectionBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
AHeroPlayerCharacter* Player = Cast<class AHeroPlayerCharacter>(OtherActor);
if (Player)
{
PlayerCharacter = Player;
if (fMoveToPlayerCurve)
{
ItemDetectionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
ProjectileMovement->Deactivate();
RotatingMovement->Deactivate();
FOnTimelineFloat TimelineProgress;
TimelineProgress.BindUFunction(this, FName("TimelineFloatReturn"));
MovementTimeline.AddInterpFloat(fMoveToPlayerCurve, TimelineProgress);
MovementTimeline.PlayFromStart();
}
else
{
UE_LOG(LogTemp, Warning, TEXT("NO CURVE ASSIGNED"));
}
}
}
Ignore the actual logic that I am doing and observe where I am creating the default subobject and where I am setting up the bindings for overlap; all this takes place in the constructor. Additionally, make sure you are including:
#include "Runtime/Engine/Classes/Components/SphereComponent.h"
You donāt show your includes, so I canāt be certain if you have it; I assume you do if the code compiles. I hope this example helps you in some way, and good luck.