Hello @kya7771 !
It looks like the collisions are well set on the editor (make sure that both actors are set to overlap with the other’s actor collision channel). But you are missing a few things in the cpp class.
The header of the actor has to look something like this
#pragma once
#include "Components/BoxComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/SphereComponent.h"
#include "MyExample.generated.h"
UCLASS()
class AMyExample : public AActor
{
GENERATED_BODY()
public:
AMyExample();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UBoxComponent* BoxCollision;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
UCapsuleComponent* CapsuleCollision;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
USphereComponent* SphereCollision;
virtual void BeginPlay() override;
UFUNCTION()
void OnAnyCollisionBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
};
For this example, I declared 3 different collision components (not a good idea to implement more than 1 per actor). BoxCollision
, CapsuleCollision
and SphereCollision
. Then I override the BeginPlay() as it will be there that we are going to subscribe our function to that event/delegate of the component OnBeginOverlap. And finally I declared a function that will trigger everytime my components overlap with something OnAnyCollisionBeginOverlap
(NOTE: The parameters of that function are extremely important if you don’t declare them. It will not work, for context I use the parameters of the delegate on its signature)
The signature that I’m talking about
/** Delegate for notification of start of overlap with a specific component */
DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_SixParams( FComponentBeginOverlapSignature, UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);
Then for the cpp file, we have this:
#include "MyExample.h"
AMyExample::AMyExample()
{
// creating the collisions components
BoxCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollisionComponent"));
SphereCollision = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollisionComponent"));
CapsuleCollision = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleCollisionComponent"));
// placing the collisions on the world
BoxCollision->SetupAttachment(RootComponent);
SphereCollision->SetupAttachment(RootComponent);
CapsuleCollision->SetupAttachment(RootComponent);
}
void AMyExample::BeginPlay()
{
Super::BeginPlay();
// subscribes my function "OnAnyCollisionBeginOverlap" to the delegate of the components "OnComponentBeginOverlap"
// this means that whenever those components begin an overlap with another component, this actor is gonna trigger the function
// "OnAnyCollisionBeginOverlap"
BoxCollision->OnComponentBeginOverlap.AddDynamic(this, &AMyExample::OnAnyCollisionBeginOverlap);
SphereCollision->OnComponentBeginOverlap.AddDynamic(this, &AMyExample::OnAnyCollisionBeginOverlap);
CapsuleCollision->OnComponentBeginOverlap.AddDynamic(this, &AMyExample::OnAnyCollisionBeginOverlap);
}
void AMyExample::OnAnyCollisionBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
// What to do on a overlap
// Your code goes here
}
And with that, it should work as intended. Hope this solves your question!