Trigger box doesn't work when spawned through code?

I made a custom trigger box in C++, a pretty basic one that’s used by an enemy when they get close to the player. It’s supposed to detect an overlap with the player character, and assign damage when it does. The only problem is, when the box is spawned by the enemy it doesn’t detect an overlap with anything. I added a debug message to the OnOverlapBegin function, but no matter what I do it never triggers. Strangely enough, when an instance of the trigger box is put directly into the game (not spawned by an enemy, just there from the start), the OnOverlapBegin function DOES work properly. So the problem doesn’t seem to be with detecting an overlap, but with the parameters of the box when it gets spawned. I’ve tried for a few hours now, and so far I haven’t gotten anything to work, so I was wondering if anybody could help me, or knew what I was doing wrong?

Code for the trigger box (edited for debugging purposes):
Header


#pragma once

#include "CoreMinimal.h"
#include "Engine/TriggerVolume.h"
#include "DamageVolume.generated.h"

UCLASS()
class TOPDOWNPROJECT_API ADamageVolume : public ATriggerVolume
{
    GENERATED_BODY()

public:
    /** Sets default values for this character's properties */
    ADamageVolume();

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

public:
    UFUNCTION()
        void OnOverlapBegin(class AActor *OverlappedActor, class AActor *OtherActor);

};


CPP


#include "DamageVolume.h"
#include "DrawDebugHelpers.h"
#include "PlayerChar.h"
#include "Kismet/GameplayStatics.h"
#include "Engine.h"

ADamageVolume::ADamageVolume()
{
    OnActorBeginOverlap.AddDynamic(this, &ADamageVolume::OnOverlapBegin);
    SetActorScale3D(FVector(0.5f, 0.5f, 0.5f));
}

void ADamageVolume::BeginPlay()
{
    DrawDebugBox(GetWorld(), GetActorLocation(), GetActorScale() * 100, FColor::Cyan, true, 1000, 0, 5);
}

void ADamageVolume::OnOverlapBegin(class AActor *OverlappedActor, class AActor *OtherActor)
{
    GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Cyan, FString::Printf(TEXT("OnOverlapBegin")));
    if (OtherActor->IsA(APlayerChar::StaticClass()))
    {
        APlayerChar *Player = Cast<APlayerChar>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
        Player->ReceiveDamage(20.0f);
    }
    Destroy();
}

Code used to spawn the trigger box (In Enemy.cpp):


FRotator SpawnRot = this->GetActorRotation();
FVector SpawnLoc = this->GetActorLocation() + (this->GetActorForwardVector() * 70.0f);
UWorld *const World = GetWorld();
if (World != NULL)
{
    ADamageVolume *Attack = World->SpawnActor<ADamageVolume>(SpawnLoc, SpawnRot);
}

Any help would be appreciated, I’m really quite stuck on this.

1 Like