Hello. I’m trying to add some simple collision code for a teapot shooting teabags inside a teacup.
This is my cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Teacup.h"
// Sets default values
ATeacup::ATeacup()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
TeacupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Teacup Mesh"));
TeacupMesh->SetupAttachment(RootComponent);
CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Collision Box"));
CollisionBox->SetupAttachment(TeacupMesh);
CollisionBox->SetBoxExtent(FVector(32.0f, 32.0f, 32.0f));
CollisionBox->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f));
CollisionBox->SetCollisionProfileName("Trigger");
}
// Called when the game starts or when spawned
void ATeacup::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("BeginPlay called for Teacup!"));
CollisionBox->OnComponentBeginOverlap.AddDynamic(this, &ATeacup::OnOverlapBegin);
CollisionBox->OnComponentEndOverlap.AddDynamic(this, &ATeacup::OnOverlapEnd);
}
// Called every frame
void ATeacup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ATeacup::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UE_LOG(LogTemp, Warning, TEXT("Entered collision"));
}
void ATeacup::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
UE_LOG(LogTemp, Warning, TEXT("Exit collision"));
}
And this is my header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "GameFramework/Actor.h"
#include "Teacup.generated.h"
UCLASS()
class LAB1PART2_2023_API ATeacup : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATeacup();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* TeacupMesh;
UPROPERTY(VisibleAnywhere)
UBoxComponent* CollisionBox;
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);
};
There is a collision box component that I plan to use to score points in the game or something of that sort. Now as much as I try, the overlap functions do not work, as I do not see a message in the log. The actor supposed to interact with them is the teabag, with set collision BlockAllDynamic. What I have tried that works is going to the Blueprint event graph and placing a blueprint with a print script. And so I know that the collision areas are not the problem. What am I doing wrong? Please help, this has frustrated me endlessly.