Unreal5 Overlap C++

Unreal5 Overlap C++

Overlap Event.

Overlap Event는 두 개의 Collider가 겹치는 순간 발생하는 이벤트입니다.

예를 들어 캐릭터와 아이템 Collider가 겹쳐지면 Overlap Event가 발생하고, 이를 통해 아이템 획득 등의 이벤트를 구현할 수 있습니다.

 

이번 글에서는 Item Actro에 SphereComponent를 추가해서 SphereComponent의 범위 내로 Character가 들어왔을 때 메시지를 통해 알려주고 추가로 sin함수를 통해 둥둥 떠있는 효과를 추가하려고 합니다.

Item Actor 생성 및 구현.

Item C++코드를 Actor로 생성합니다.

먼저 PrimitiveComponent.h로 들어가서 overlap어쩌고 있는부분의 매개변수를 복사해 옵니다.

Item.h의 코드는 이렇게 작성하였습니다.

복사한 매개변수를 넣고 수정좀 해서 OnSphereOverlap, OnSphereOverlapOut함수를 작성했습니다.

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	UFUNCTION()
	virtual void OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	virtual void OnSphereOverlapOut(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* ItemMesh;
	UPROPERTY(VisibleAnywhere)
	USphereComponent* Sphere;
private:
	float TransformSin();
	float RunningTime;

Item.cpp는 먼저 생성자에 가서 ItemMesh, Sphere를 추가해 줍니다.

AItem::AItem()
{
 	// 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;
	//#include "Components/StaticMeshComponent.h"
	ItemMesh = CreateDefaultSubobject<UStaticMeshComponent>("ItemMesh");
	ItemMesh->SetupAttachment(GetRootComponent());
	//#include "Components/SphereComponent.h" 추가.
	Sphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
	Sphere->SetupAttachment(ItemMesh);
}

Item.cpp의 BeginPlay에서 Event를 추가해 줍니다.

void AItem::BeginPlay()
{
	Super::BeginPlay();
	Sphere->OnComponentBeginOverlap.AddDynamic(this, &AItem::OnSphereOverlap);
	Sphere->OnComponentEndOverlap.AddDynamic(this, &AItem::OnSphereOverlapOut);
}

요건 Overlap이랑은 별로 관계없지만 그냥 넣고 싶어서 넣은 내용...ㅎㅎ

Sin함수는 주기함수라 Z 축에만 적용해서 아이템이 둥둥 떠있는 효과를 줍니다.

void AItem::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	AddActorWorldOffset(FVector(0,0,TransformSin()));
	RunningTime += DeltaTime;
}
float AItem::TransformSin()
{
	return 0.25f * FMath::Sin(RunningTime * 5.f);
}

Begin Overlap과 End Overlap.

Overlap이벤트 작동 확인을 위한 코드밖에 없습니다.

1초 동안 들어오면 파란색, 나가면 파란색으로 이름을 표시합니다.

void AItem::OnSphereOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	const FString _Name = OtherActor->GetName();
	if(GEngine){
		GEngine->AddOnScreenDebugMessage(1, 1.f, FColor::Blue, _Name);
	}
}

void AItem::OnSphereOverlapOut(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	const FString _Name =  OtherActor->GetName();
	if (GEngine) {
		GEngine->AddOnScreenDebugMessage(2, 1.f, FColor::Red, _Name);
	}
}

결과.

이렇게 작성한 코드를 블루프린트로 상속받아서 item Mesh적당한걸로 찾아서 추가하고, 원하는 크기로 Sphere 설정해 주었습니다. 아래 사진에 있는 칼 보이죠? 

결과는 이렇습니다. 잘 나오네요.

 

Designed by JB FACTORY