Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USphereComponent and Overlap Events

I seem to be misunderstanding how to implement overlap events on my components. I've created a USphereComponent to follow my character. It's designed to fire overlap events on other nearby Actors that are within the player's reach:

AScrollsCharacter::AScrollsCharacter()
{
    ...Redacted irellevant code...

    //Create activate trigger radius
    USphereComponent* activateRadius = CreateDefaultSubobject<USphereComponent>(TEXT("Activate Radius"));
    activateRadius->InitSphereRadius(ACTIVATE_RADIUS);
    activateRadius->bGenerateOverlapEvents = true;
    activateRadius->SetupAttachment(RootComponent);
    activateRadius->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
    activateRadius->OnComponentBeginOverlap.AddDynamic(this, &AScrollsCharacter::OnOverlapActivateSphere);
    activateRadius->bHiddenInGame = false;
}

void AScrollsCharacter::OnOverlapActivateSphere(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("We got a collision."));
}

The sphere component attaches without error, and the radius is drawn on screen at the correct size. However, when I bring the radius towards other actors in game and cause them to overlap, the event doesn't appear to be firing.

All actors involved have the the Generate Overlap Events flag set to true.

Can someone help me understand what I'm missing in this setup?

Note: The debug bounds of the sphere component are colored orange while in the editor, but turn red when the game is running. Is that color change meaningful?

like image 837
Nathan Wiles Avatar asked Nov 09 '22 09:11

Nathan Wiles


1 Answers

If you're still trying to fix this problem, maybe try using

activateRadius->SetSphereRadius(ACTIVATE_RADIUS);

instead of

activateRadius->InitSphereRadius(ACTIVATE_RADIUS);

According to the docs, the function you're using, InitSphereRadius:

Sets the sphere radius without triggering a render or physics update.

I'm pretty sure you want that physics update.

like image 82
jeevcat Avatar answered Nov 15 '22 11:11

jeevcat