[C++] 줌인, 줌아웃 구현

Woogle·2022년 11월 21일
0

언리얼 엔진 5

목록 보기
33/59

📄 Build.cs

  • UMG 모듈을 추가한다.
using UnrealBuildTool;

public class MyTPS : ModuleRules
{
	public MyTPS(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });
	}
}

📄 C++

  • BeginPlay : 게임 시작 시 UserWidget을 생성한다.

  • SetupPlayerInputComponent : 키 입력시 호출되는 함수(BindAction)에서 위젯을 Viewport에서 붙이거나 제거한다.

✏️ TPSPlayer.h

	UPROPERTY(EditAnywhere)
	TSubclassOf<class UUserWidget> sniperUIFactory;

	class UUserWidget* sniperUI;

	void OnActionZoomIn();
	void OnActionZoomOut();

✏️ TPSPlayer.cpp

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

	// ...
	sniperUI = CreateWidget(GetWorld(), sniperUIFactory);
}

void ATPSPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// ...
	PlayerInputComponent->BindAction(TEXT("Zoom"), IE_Pressed, this, &ATPSPlayer::OnActionZoomIn);
	PlayerInputComponent->BindAction(TEXT("Zoom"), IE_Released, this, &ATPSPlayer::OnActionZoomOut);
}

void ATPSPlayer::OnActionZoomIn()
{
	if (sniperUI->IsInViewport() == false)	// 안전장치
	{
		sniperUI->AddToViewport();
	}
	cameraComp->SetFieldOfView(30.f);	// FOV 변경
}

void ATPSPlayer::OnActionZoomOut()
{
	if (sniperUI->IsInViewport() == true)	// 안전장치
	{
		sniperUI->RemoveFromParent();
	}
	cameraComp->SetFieldOfView(90.f);	// FOV 초기화
}

📄 Blueprint

  • WBP_Sniper를 만든다.
  • BP_TPSPlayer에서 UI를 지정해준다.
profile
노력하는 게임 개발자

0개의 댓글