본문 바로가기
언리얼 엔진/기초

[Unreal Engine5] 무기 매커니즘 만들기(1) - Box Trace

by TyT. 2023. 8. 24.
반응형

GitHub: https://github.com/tyt0815

 

tyt0815 - Overview

tyt0815 has 4 repositories available. Follow their code on GitHub.

github.com

Notion: https://www.notion.so/tyt0815/a16514d41cb240f08aa19fe5b4c0ab86?pvs=4 

 

언리얼 엔진

Unreal Engine 5 C++ The Ultimate Game Developer Course

www.notion.so

※노션에 기존에 작성하던 글이 있으니 참고

 

 

무기 장착

https://www.notion.so/tyt0815/a16514d41cb240f08aa19fe5b4c0ab86?pvs=4 

 

언리얼 엔진

Unreal Engine 5 C++ The Ultimate Game Developer Course

www.notion.so

노션에 56번글 The Weapon Class태그가 붙은 부분부터 읽으면 무기 장착에 대한 내용을 알 수 있습니다.

Enemy Collision 설정

에너미의 콜리전을 설정해 줍니다.

ABaseEnemy::ABaseEnemy()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	USkeletalMeshComponent* MeshComponent = GetMesh();
	MeshComponent->SetGenerateOverlapEvents(true);
	MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	MeshComponent->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
	MeshComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
	MeshComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
	MeshComponent->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
	
}

위 클래스를 상속하는 블루프린트의 메시컴포넌트의 콜리전이 아래와 같이 설정되게 될 것 입니다.

여기서 주의할 점은 메시컴포넌트의 스켈레탈 메시가 피직스를 가지고 있는지 확인을 해야 합니다.(제가 이거때문에 몇시간을 개고생함)

무기 클래스 메커니즘

무기 클래스의 생성자를 다음과 같이 해줍니다.

AWeapon::AWeapon()
{
	// 무기 히트박스
	WeaponBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Box"));
	WeaponBox->SetupAttachment(GetRootComponent());
	WeaponBox->SetGenerateOverlapEvents(true);
	WeaponBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	WeaponBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
	WeaponBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);

	BoxTraceStart = CreateDefaultSubobject<USceneComponent>(TEXT("BoxTraceStart"));
	BoxTraceStart->SetupAttachment(GetRootComponent());
	BoxTraceEnd = CreateDefaultSubobject<USceneComponent>(TEXT("BoxTraceEnd"));
	BoxTraceEnd->SetupAttachment(GetRootComponent());
}

위 코드는 순서대로 다음과 같은 일을 합니다.

  • 무기의 히트박스를 만듦
  • 히트박스의 콜리전을 설정함
  • 히트박스가 적과 Overlap될시 공격을 할 적인지 확인할 BoxTrace의 시작점과 도착점을 생성함.

그리고 히트박스가 액터와 오버랩될때 실행될 함수를 만들어 줍니다.

void AWeapon::OnBoxOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	const FVector Start = BoxTraceStart->GetComponentLocation();
	const FVector End = BoxTraceEnd->GetComponentLocation();

	TArray<AActor*> ActorsToIgnore;
	ActorsToIgnore.Add(this);
	FHitResult BoxHit;
	UKismetSystemLibrary::BoxTraceSingle(
		this, 
		Start, 
		End, 
		FVector(5.f, 5.f, 5.f),
		BoxTraceStart->GetComponentRotation(),
		ETraceTypeQuery::TraceTypeQuery1,
		false,
		ActorsToIgnore,
		EDrawDebugTrace::ForDuration,
		BoxHit,
		true
		);
}

오버랩이 되면 박스 트레이스를 실행하게 합니다.

마지막으로 비긴플레이에서 히트박스에 오버랩함수를 콜백함수로 등록해 줍니다.

void AWeapon::BeginPlay()
{
	Super::BeginPlay();

	WeaponBox->OnComponentBeginOverlap.AddDynamic(this, &AWeapon::OnBoxOverlap);
}

컴파일하고 무기클래스를 상속하는 블루프린트로 와서 히트박스를 설정하고 히트박스의 끝과 끝에 박스트레이스 Start, End를 위치시킵니다.

컴파일하고 실행해서 에너미를 공격해 봅시다.

 

반응형