So I made a Pickup Actor following a video (link: Unreal Engine C++ Tutorial - Episode 2: Pickups - YouTube) and it work fine, but I want the player to be destroyed when I collide with the Actor and not the other away around, preferably in C++.
Here’s the code for the Pickup Actor .cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Pickup.h"
#include "CGPRCharacter.h"
#include "Components/ShapeComponent.h"
#include "Components/BoxComponent.h"
#include "Components/MeshComponent.h"
#include "Components/StaticMeshComponent.h"
// Sets default values
APickup::APickup()
{
// 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;
PickupRoot = CreateDefaultSubobject<USceneComponent>(TEXT("PickupRoot"));
RootComponent = PickupRoot;
PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
PickupMesh->AttachToComponent(PickupRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
PickupBox = CreateDefaultSubobject<UBoxComponent>(TEXT("PickupBox"));
PickupBox->SetWorldScale3D(FVector(1.0f, 1.0f, 1.0f));
PickupBox->SetGenerateOverlapEvents(true);
PickupBox->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPlayerEnterPickupBox);
PickupBox->AttachToComponent(PickupRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called when the game starts or when spawned
void APickup::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void APickup::OnPlayerEnterPickupBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Destroy();
}
and here’s the one for the .h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Pickup.generated.h"
UCLASS()
class CGPR_API APickup : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APickup();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
USceneComponent* PickupRoot;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* PickupMesh;
UPROPERTY(EditAnywhere)
class UBoxComponent* PickupBox;
UFUNCTION()
void OnPlayerEnterPickupBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};