[UE5] Lena: Dev Diary #5 - Ammo 시스템 구현과 아이템 획득 기능 추가

ChangJin·2024년 6월 18일
0

Unreal Engine5

목록 보기
71/102
post-thumbnail

2024.06.18

깃허브!풀리퀘!
https://github.com/ChangJin-Lee/SimpleShooter https://github.com/ChangJin-Lee/Project-Lena/pull/4

이번 포스팅에서는 탄환 수를 관리하고, 아이템을 통해 탄환을 획득하는 시스템을 구현한 과정을 다룹니다. HUD에서 플레이어의 탄환 수를 표시하고, Pickup_Item 클래스를 통해 아이템을 획득할 수 있게 만들었습니다.


진행상황

  • ✅ 탄환 수 관리 시스템 구현
  • ✅ HUD에서 탄환 수 표시
  • ✅ 아이템 획득 시스템 구현
  • Pickup_Item 클래스 추가



탄환 수 관리 시스템 구현

  • HUD에 탄환 수를 표시하는 기능을 추가했습니다.
  • 체력 옆에 현재 남은 탄환 수를 Ammo로 표시합니다.

HUD에서 탄환 수를 표시하는 모습
HUD에서 탄환 수를 업데이트하는 블루프린트 노드

  • 탄환 수 만큼만 총을 발사할 수 있도록 시스템을 구현했습니다.
  • Player 클래스에서 죽음과 관련된 Health <= 0 조건처럼 총에서 Ammo <= 0 조건을 추가하여 탄환이 비었을 때 더 이상 발사되지 않도록 했습니다.
  • USoundBase* 타입의 AmmoEmptySound를 선언해 탄환이 비었을 때 나는 소리를 재생합니다.

void AGun::PullTrigger()
{
	if(IsEmpty())
	{
		UGameplayStatics::PlaySoundAtLocation(GetWorld(), AmmoEmptySound, GetActorLocation());
		return;
	}
    
	UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, Mesh, TEXT("MuzzleFlashSocket"));
	UGameplayStatics::SpawnSoundAttached(MuzzleSound, Mesh, TEXT("MuzzleFlashSocket"));
	
	FHitResult HitResult;
	FVector ShotDirection;

	bool bSuccess = GunTrace(HitResult, ShotDirection);
	if(bSuccess)
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactEffect, HitResult.Location, ShotDirection.Rotation());
		UGameplayStatics::PlaySoundAtLocation(GetWorld(), ImpactSound, HitResult.Location);

		AActor* DamagedActor = HitResult.GetActor();
		if(DamagedActor)
		{
			FPointDamageEvent DamageEvent(Damage, HitResult, ShotDirection, nullptr);
			AController *OwnerController = GetOwnerController(); 
			DamagedActor->TakeDamage(Damage, DamageEvent, OwnerController, this);
			Ammo--;
		}
	}
}


아이템 획득 시스템 구현

  • Pickup_Item 클래스를 추가하여 아이템을 획득할 수 있도록 만들었습니다.

아이템 획득 시스템 구현탄환 수 증가 모습

  • Pickup_Item 클래스는 스켈레탈 메시, 메시 컴포넌트, 박스 콜라이더를 BeginPlay에서 생성하도록 코딩했습니다.

#include "Pickup_Item.h"
#include "Components/BoxComponent.h"

APickup_Item::APickup_Item()
{
	PrimaryActorTick.bCanEverTick = true;
	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	MeshComponent->SetupAttachment(RootComponent);

	SkeletalMeshComponent = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMeshComponent"));
	SkeletalMeshComponent->SetupAttachment(MeshComponent);

	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComponent"));
	BoxComponent -> SetupAttachment(MeshComponent);
}

void APickup_Item::BeginPlay()
{
	Super::BeginPlay();
	
}

void APickup_Item::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

  • Pickup_Item 클래스를 기반으로 만든 BP_Pickup_Item 블루프린트는 오버랩되었을 때 플레이어 캐릭터가 들고 있는 무기의 탄환 수를 증가시켜줍니다.

오버랩 시 특정 함수를 실행하는 블루프린트 노드


이번 포스팅이 도움이 되셨길 바랍니다. 다음 포스팅도 많은 기대 부탁드립니다!

  • 도움이 되었던 링크들

https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/Physics/Collision/Overview/
https://forums.unrealengine.com/t/how-can-i-make-trigger-overlapping-persistent/558051/4
https://forums.unrealengine.com/t/only-overlap-with-a-specific-component-of-third-person-character/650243
https://forums.unrealengine.com/t/how-to-check-which-actor-overlap-with-player/569063/5
https://forums.unrealengine.com/t/cast-to-scenecomponent/717539

profile
Unreal Engine 클라이언트 개발자

0개의 댓글