캡스톤 졸업작품/언리얼 엔진 4

[캡스톤 디자인] VR게임 제작하기 - VR Pawn 만들기

rlaghrud1234 2023. 1. 6. 15:05

※본 글은 전자공학부 캡스톤 디자인 작품인 "VR 체험형 소프트웨어 제작"주제로 진행한느 내용을 정리하기 위한 글로, 개발 과정에 전반적인 흐름과 코드를 포스팅 하기 위해 작성 됐다. 혹여, VR 관련된 프로젝트를 하는 분들이 계시거나 UE4를 잘 아는 분이 계시면 해당 포스트에서 틀린 곳이나 수정해야 할 부분이 있으면 말씀해주시면 감사하겠습니다!

 

그리고 해당 프로젝트는 C++과 블루프린트의 혼용으로 진행할 예정입니다!


1. VR Pawn C++ 클래스를 생성한다.

TP랑은 다르게 VR은 Pawn 클래스를 만들어줘야 한다.

C++ 클래스를 만들어주고 블루프린트에 얹어주는 과정으로 진행하고자 한다.

VRPawn C++ 클래스

 

2. VR Pawn C++ 기본 세팅을 해준다.

1. VRPawn.h

헤더파일에 변수에 해당하는 것들을 모두 선언해준다. 자세한 내용은 언리얼 엔진 문서를 참고하면 된다.

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "Components/TextRenderComponent.h"
#include "MotionControllerComponent.h"
#include "VRPawn.generated.h"

UCLASS()
class CAPSTON_1_API AVRPawn : public APawn
{
GENERATED_BODY()

public:
// Sets default values for this pawn's properties
AVRPawn();

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

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

//Setting VR
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
USceneComponent* VRTrackingCenter;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UCameraComponent* VRCameraComponent;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UTextRenderComponent* OutputText;

//MotionControllerComponent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UMotionControllerComponent* LeftController;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UMotionControllerComponent* RightController;

//StaticMeshComponent
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UStaticMeshComponent* LeftSword;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
USkeletalMeshComponent* LeftHand;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
UStaticMeshComponent* RightSword;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = __hide)
USkeletalMeshComponent* RightHand;
};

2. VRPawn.cpp

선언에 해당하는 소스코드들을 cpp 파일에 작성한다. 자세한 내용은 언리얼 엔진 문서를 참고하면 된다.

// Fill out your copyright notice in the Description page of Project Settings.

#include "VRPawn.h"
#include "MotioncontrollerComponent.h"
#include "Components/SceneComponent.h"
#include "Camera/CameraComponent.h"

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

	//가장 상단이 되는 부분
	VRTrackingCenter = CreateDefaultSubobject<USceneComponent>(TEXT("VRTrackingCenter"));
	VRCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("VRCameraComponent"));
	OutputText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("OutputText"));

	//좌측 컨트롤러 세팅
	LeftController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("LeftController"));
	LeftController->SetTrackingSource(EControllerHand::Left);
	LeftSword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("LeftSword"));
	LeftHand = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("LeftHand"));

	//우측 컨트롤러 세팅
	RightController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("RightConterller"));
	RightController->SetTrackingSource(EControllerHand::Right);
	RightSword = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RightSword"));
	RightHand = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("RightHand"));

	//상단이 되는 부분에 카메라 세팅
	VRCameraComponent->SetupAttachment(VRTrackingCenter);
	OutputText->SetupAttachment(VRCameraComponent);

	//좌측에 해당하는 Component들을 상속시켜주는 과정
	LeftController->SetupAttachment(VRTrackingCenter);
	LeftHand->SetupAttachment(LeftController);
	LeftSword->SetupAttachment(LeftHand);

	//우측에 해당하는 Component들을 상속시켜주는 과정
	RightController->SetupAttachment(VRTrackingCenter);
	RightHand->SetupAttachment(RightController);
	RightSword->SetupAttachment(RightHand);
}

// Called when the game starts or when spawned
void AVRPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AVRPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AVRPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

 

3. VR_Player 블루프린트를 생성하고 상속된 결과를 확인한다.

상속이 됐다.

 

4. 뷰포트에서 Static Mesh들을 모두 넣어준다.

넣어줬다

 

5. VR모드로 실행해주고 결과를 확인한다.

YES!

 

6. 앞으로 할일

1. Grib 만들기

2. Grib 애니메이션 만들기

3. 실제로 집어서 칼 들어보기