Choose your operating system:
Windows
macOS
Linux
-
카메라 제어로 무엇을 할 것인지 결정하고, 그에 맞게 입력을 구성해 줘야 합니다. 이 프로젝트에서는 마우스 오른쪽 버튼을 누르면 따라다니는 거리를 줄이고 시야를 확대하도록 하겠습니다. 마우스로 보는 각도를, WASD 키로 폰 의 이동을 제어하도록도 합시다. 그러기 위해, 언리얼 엔진 에디터의 편집 드롭다운 메뉴의 프로젝트 세팅 을 열겠습니다.
-
액션 매핑 하나와 축 매핑 넷을 다음과 같이 정의해 줘야 합니다:
액션 매핑
ZoomIn
오른쪽 마우스 버튼
축 매핑
MoveForward
W
1.0
S
-1.0
MoveRight
A
-1.0
D
1.0
CameraPitch
MouseY
1.0
CameraYaw
MouseX
1.0
입력을 정의했으니, 그에 반응하는 코드를 작성해 줘야 합니다. 다음 작업을 위해 Visual Studio 로 돌아가겠습니다.
작업중 코드
PawnWithCamera.h
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"
UCLASS()
class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APawnWithCamera();
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;
protected:
UPROPERTY(EditAnywhere)
USpringArmComponent* OurCameraSpringArm;
UCameraComponent* OurCamera;
};
PawnWithCamera.cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
#include "HowTo_PlayerCamera.h"
#include "PawnWithCamera.h"
// Sets default values
APawnWithCamera::APawnWithCamera()
{
// 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;
//Create our components
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
OurCameraSpringArm->SetupAttachment(RootComponent);
OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
OurCameraSpringArm->TargetArmLength = 400.f
OurCameraSpringArm->bEnableCameraLag = true;
OurCameraSprintArm->CameraLagSpeed = 3.0f
OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);
//Take control of the default Player
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void APawnWithCamera::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APawnWithCamera::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
}