Download

Not firing: OnComponentBeginOverlap.AddDynamic()

I’m trying to create a “pressure plate” kind of setup (derived from AActor), where a BP event will fire when the player moves onto a square but nothing seems to be happening. Where am I going wrong here?

.h


UPROPERTY(EditAnywhere, Category = "Pressure plate")
UBoxComponent* PlateTrigger;

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

UFUNCTION()
void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);


.cpp


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

    SetReplicates(true);

    PlateMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Pressure plate mesh"));
    SetRootComponent(PlateMesh);

    PlateTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("Pressure plate trigger"));
    PlateTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABasePressurePlate::OnOverlapBegin);
    PlateTrigger->OnComponentEndOverlap.AddDynamic(this, &ABasePressurePlate::OnOverlapEnd);
}

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

void ABasePressurePlate::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    UE_LOG(LogTemp, Warning, TEXT("OnOverlapEnd()"))
}


You need to set the collision profile and enable the queries.



PlateTrigger->SetCollisionEnabled(ECollisionEnabled::QueryOnly); // We want overlaps.
PlateTrigger->SetCollisionProfileName("Pawn"); // We want to be on the "Pawn" collision channel


See: Collision Overview | Unreal Engine Documentation

Thanks for the reply, Matt.

I tried that but no joy… Added a line to set the collision response too, but didn’t help. The player actually collides with it (pic below). This is what it looks like now:


    PlateTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("Pressure plate trigger"));
    PlateTrigger->SetupAttachment(RootComponent);
    PlateTrigger->SetGenerateOverlapEvents(true);
    PlateTrigger->SetCollisionResponseToAllChannels(ECR_Overlap);
    PlateTrigger->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
    PlateTrigger->SetCollisionProfileName("Pawn");
    PlateTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABasePressurePlate::OnOverlapBegin);
    PlateTrigger->OnComponentEndOverlap.AddDynamic(this, &ABasePressurePlate::OnOverlapEnd);

I tried using [FONT=courier new]SetCollisionProfileName(“OverlapAll”) instead, which stops the player colliding with the BoxComponent, but still doesn’t fire the OnOverlapBegin() events.

Capture.PNG

Never mind, it was my own fault. While I was trying things out I had removed UFUNCTION() from the .h file. I’ve put it back in and now it’s working as expected. Thanks again.