Choose your operating system:
Windows
macOS
Linux
언리얼 엔진 4 에 처음이신 경우, 프로그래밍 퀵스타트 튜토리얼
-
기본 코드 프로젝트에 시작용 콘텐츠를 포함시켜 "HowTo_PlayerCamera" 라는 이름으로 만드는 것으로 시작합니다. Pawn 클래스부터 먼저 만들어 줍시다. 이 튜토리얼에서는 새로운 Pawn 클래스 이름을 "MyPawn" 으로 하겠습니다.
Pawn , 폰이란 사람 플레이어 또는 AI 의 제어를 받을 것으로 디자인된 액터
-
우선 게임 시작시 MyPawn 이 자동으로 플레이어 입력에 반응하도록 설정해 주겠습니다. Pawn 클래스에는 초기화 도중 그 작업을 대신해 주는 설정가능 변수가 제공됩니다.
MyPawn.cpp
에서 다음 코드를 AMyPawn::AMyPawn 에 추가합니다:// 이 폰을 가장 빠른 번호의 플레이어가 조종하도록 설정합니다 AutoPossessPlayer = EAutoReceiveInput::Player0;
-
다음으로 기본적인 컴포넌트 를 몇 가지 만듭니다. 코드로 컴포넌트 를 추가 및 관리하는 법은 물론 자주 보이게 되는 컴포넌트 유형에 대해서는 컴포넌트와 콜리전 문서를 참고하세요. 컴포넌트 의 기록 유지를 위해 다음 코드를
MyPawn.h
내 클래스 정의 아래 추가해 주겠습니다:UPROPERTY(EditAnywhere) USceneComponent* OurVisibleComponent;
UPROPERTY 가 붙어있어 언리얼 엔진
그리고
MyPawn.cpp
로 돌아와서 다음 코드를 AMyPawn::AMyPawn 에 추가합니다:// 무언가를 붙일 더미 루트 컴포넌트를 만듭니다 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent")); // 카메라와 보이는 오브젝트를 만듭니다 UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera")); OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent")); // 루트 컴포넌트에 카메라와 보이는 오브젝트를 붙입니다. 카메라를 이격 및 회전시킵니다. OurCamera->SetupAttachment(RootComponent); OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f)); OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f)); OurVisibleComponent->SetupAttachment(RootComponent);
-
변경내용을 저장하고 Visual Studio 의 Build 명령 또는 언리얼 엔진 에디터의 컴파일 버튼으로 컴파일하면 됩니다.
Pawn 이 게임 입력에 반응하도록 커스터마이징했으니, 그 입력을 정의해 줘야 합니다. 그러기 위해 언리얼 엔진 에디터에서 프로젝트의 입력 세팅 환경설정을 해 주겠습니다.
작업중 코드
MyPawn.h
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Pawn.h"
#include "MyPawn.generated.h"
UCLASS()
class HOWTO_PLAYERINPUT_API AMyPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values
AMyPawn();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
};
MyPawn.cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#include "HowTo_PlayerInput.h"
#include "MyPawn.h"
// Sets default values
AMyPawn::AMyPawn()
{
// 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;
// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;
// Create a dummy root component we can attach things to.
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Create a camera and a visible object
UCameraComponent* OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
// Attach our camera and visible object to our root component. Offset and rotate the camera.
OurCamera->SetupAttachment(RootComponent);
OurCamera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
OurCamera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
OurVisibleComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
}