GameMode
에서 사용자의 입력을 처리하는 클래스.Enhanced Input
시스템을 이용하면 액션, 축 매핑을 보다 체계적으로 설정할 수 있음.SetupInputComponent()
함수를 오버라이드해서 구현UnPossess()
함수로 Pawn과 연결을 해제하고 다른 Pawn을 제어할 수도 있음.PlayerController
클래스를 상속받아 작성.// .h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Cp2PlayerController.generated.h"
UCLASS()
class CHAPTER2_API ACp2PlayerController : public APlayerController
{
GENERATED_BODY()
};
// .cpp
#include "Cp2PlayerController.h"
GameMode
에 새로 작성한 CP2PlayerController
적용DefaultPawnClass
를 지정했던 것과 동일하게 할당// .cpp
#include "Cp2GameMode.h"
#include "Cp2Character.h"
#include "Cp2PlayerController.h"
ACp2GameMode::ACp2GameMode()
{
DefaultPawnClass = ACp2Character::StaticClass(); // 객체를 생성하지 않고 클래스를 반환해서 넣어줄 것임.
PlayerControllerClass = ACp2PlayerController::StaticClass();
}
Cp2PlayerController
클래스를 BP로 래핑(Wrap).GameMode
와 거기에 할당했던 BP 액터들이 Spawn 돼 있음.Enhanced Input
시스템을 제공함.Enhanced Input
시스템은 입력 설정을 입력 맵 Input Mapping Context (IMC)
과입력 액션 Input Action (IA)
이라는 개념으로 나눠 관리Input Action (IA)
은 캐릭터의 이동, 점프, 발사 등과 같이 특정 동작을 추상화한 단위.Content Browser에서 우클릭 > Input > Input Action을 선택
Bool
(참/거짓)Axis1D
(1차원 축 값)Axis2D
(2차원 축 값)Axis3D
(3차원 축 값)등등
이동, 점프, 시점 회전, 스프린트 버튼 등을 적용해볼 것.
Axis2D (Vector2D)
로 설정해서 입력값을 Vector2D로 받도록 설정.IA_Move
는 상시 활성화 상태 에서 wasd 입력으로 구현할 것이므로Bool
로 설정.Axis2D
로 설정Bool
로 설정.Input Mapping Context (IMC)
는 여러 개의 IA들을 한데 모아놓은 매핑 설정 파일 Swizzle Input Axis Values
: 입력 축 (Axis)을 변환하거나 재구성하는 기능Negate
Modifier 추가Negate
설정Axis2D
로 지정한 X축, Y축 값을 게임 내 좌표계에 맞춰 회전에 반영Negate
적용// .h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Cp2PlayerController.generated.h"
class UInputMappingContext; // IMC 전방 선언
class UInputAction; // IA 전방 선언
UCLASS()
class CHAPTER2_API ACp2PlayerController : public APlayerController
{
GENERATED_BODY()
public:
ACp2PlayerController();
// 에디터에서 쓸 IMC
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Input")
UInputMappingContext* InputMappintContext;
// IA_Move 변수
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UInputAction* MoveAction;
// IA_Jump 변수
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UInputAction* JumpAction;
// IA_Look 변수
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UInputAction* LookAction;
// IA_Sprint 변수
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UInputAction* SprintAction;
protected:
virtual void BeginPlay() override;
};
// .cpp
#include "Cp2PlayerController.h"
#include "EnhancedInputSubsystems.h"
ACp2PlayerController::ACp2PlayerController()
: InputMappintContext(nullptr),
MoveAction(nullptr),
JumpAction(nullptr),
LookAction(nullptr),
SprintAction(nullptr)
{
}
Local Player Subsystem
을 통해 Input Mapping Context를 활성화하거나 비활성화할 수 있음.Local Player Subsystem
UEnhancedInputLocalPlayerSubsystem
은 Local Player에 부착되어 해당 플레이어가 사용할 입력 매핑(IMC)을 관리BeginPlay()
에서 IMC를 활성화해볼 것#include "Cp2PlayerController.h"
#include "EnhancedInputSubsystems.h"
ACp2PlayerController::ACp2PlayerController()
: InputMappintContext(nullptr),
MoveAction(nullptr),
JumpAction(nullptr),
LookAction(nullptr),
SprintAction(nullptr)
{
}
void ACp2PlayerController::BeginPlay()
{
Super::BeginPlay();
// imc 활성화
if (ULocalPlayer* localPlayer = GetLocalPlayer())
{
if (UEnhancedInputLocalPlayerSubsystem* subSys =
localPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
{
if(InputMappintContext)
{
subSys->AddMappingContext(InputMappintContext, 0); // 우선 순위 0 설정
}
}
}
}
#include "EnhancedInputSubsystems.h"
GetLocalPlayer()
GetSubsystem<UEnhancedInputLocalPlayerSubsystem>()
AddMappingContext()
, RemoveMappingContext()
등을 호출해서 입력 매핑을 동적으로 제어할 수 있음.AddMappingContext()