• 플레이어에게 무기를 공급할 아이템 상자를 제작한다.
  • 트리거를 부모로함
  • SetBoxExtent()-> 박스 프레임 생성
//h
#include "KGame.h"
#include "GameFramework/Actor.h"
#include "ABItemBox.generated.h"

UCLASS()
class KGAME_API AABItemBox : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AABItemBox();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;
public:
    UPROPERTY(VisibleAnywhere, Category = Box)
        UBoxComponent* Trigger;

    UPROPERTY(VisibleAnywhere, Category = Box)
        UStaticMeshComponent* Box;
};



//cpp
AABItemBox::AABItemBox()
{
    // 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;

    Trigger = CreateDefaultSubobject<UBoxComponent>(TEXT("TRIGGER"));
    Box = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BOX"));

    RootComponent = Trigger;
    Box->SetupAttachment(RootComponent);

    Trigger->SetBoxExtent(FVector(40.0f, 42.0f, 30.0f));                //박스 틀 생성
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SM_BOX(TEXT(
        "/Game/InfinityBladeGrassLands/Environments/Breakables/StaticMesh/Box/SM_Env_Breakables_Box1.SM_Env_Breakables_Box1"));

    if (SM_BOX.Succeeded())
    {
        Box->SetStaticMesh(SM_BOX.Object);
    }
    Box->SetRelativeLocation(FVector(0.0f, -3.5f, -30.0f));
}

 

오브젝트 채널 추가 후 프리셋 추가, 캐릭터도 박스랑 겹칩으로 설정

 

겹칩으로 설정을 하면 Overlap 이벤트를 처리 할수 있게 된다 OnComponentBeginIverlap 라는 델리게이트가 선언되 있다

//.h
virtual void PostInitializeComponents() override;
private:
    UFUNCTION()
        void OnCharacterOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};

//cpp
AABItemBox::AABItemBox()
{
...
    Trigger->SetCollisionProfileName(TEXT("ItemBox"));
    Box->SetCollisionProfileName(TEXT("NoCollision"));
}

void AABItemBox::PostInitializeComponents()
{
    Super::PostInitializeComponents();
    Trigger->OnComponentBeginOverlap.AddDynamic(this, AABItemBox::OnCharacterOverlap);
}

void AABItemBox::OnCharacterOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, 
    bool bFromSweep, const FHitResult& SweepResult)
{

}

 

+ Recent posts