GitHub: https://github.com/tyt0815
tyt0815 - Overview
tyt0815 has 4 repositories available. Follow their code on GitHub.
github.com
Notion: https://www.notion.so/tyt0815/a16514d41cb240f08aa19fe5b4c0ab86?pvs=4
언리얼 엔진
Unreal Engine 5 C++ The Ultimate Game Developer Course
www.notion.so
※노션에 기존에 작성하던 글이 있으니 참고
Enhanced Input
기존의 입력방식을 사용하기 위해 프로젝트 세팅으로 가면 다음과 같은 문구를 확인할 수 있다.
즉 기존의 매핑 방식은 deprecated되고 5.1버전 부터 추가된 향상된 입력을 사용하라는 것이다.
향상된 입력으로 캐릭터 움직임과 카메라 회전을 구현해 보자.
먼저 입력 액션을 만들어 준다.
사진과 같이 Move, Look, Jump 세가지 입력 액션을 만들었다. 각각 내용은 아래와 같다.
값 타입을 보면 Move와 Look은 2차원 벡터, Jump는 bool타입이다.
다음은 입력 매핑 컨텍스트다.
이전에 만든 입력 매핑들을 키에 할당 하는 것이다. 프로젝트 세팅에서 매핑할때와 비슷하다고 생각하면 된다. 각각의 모디파이어를 수정을 제대로 하는 것을 잊지 말자. 사용된 모디파이어에 대해 언리얼 엔진의 공식 문서에서 가져오자면 아래와 같다.
컨텍스트도 만들었다면 이제 코드로 적용해 보자.
먼저 필요한 변수들을 선언해 준다.
public:
UPROPERTY(EditAnywhere, Category = Input)
UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, Category = Input)
UInputAction* JumpAction;
UPROPERTY(EditAnywhere, Category = Input)
UInputAction* LookAction;
UPROPERTY(EditAnywhere, Category = Input)
UInputAction* MoveAction;
protected:
void Move(const FInputActionInstance& Instance);
void Look(const FInputActionInstance& Instance);
입력 매핑 컨텍스트와 입력 액션을 받아줄 변수를 만들고, 각각 키와 바인딩 될 함수를 선언한다.
필요한 헤더파일을 소스코드에 include한다.
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
먼저 BeginPlay()에서 현재 컨텍스트를 우리가 만든 컨텍스트로 변경한다.
void ABasePlayerCharacter::BeginPlay()
{
Super::BeginPlay();
APlayerController* PlayerController = Cast<APlayerController>(Controller);
if (PlayerController != nullptr)
{
UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem< UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());
if(Subsystem != nullptr)
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
그리고 함수들을 바인딩 해준다.
void ABasePlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (Input != nullptr)
{
Input->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
Input->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
Input->BindAction(LookAction, ETriggerEvent::Triggered, this, &ABasePlayerCharacter::Look);
Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ABasePlayerCharacter::Move);
}
}
여기서 주의할 점은 아까 함수를 선언할때도 Jump를 따로 선언하지 않았는데, 기본적으로 ACharacter클래스에 점프가 구현이 되어있다. 따라서 그 함수를 사용한다.
이제 Move와 Look함수를 구현한다.
void ABasePlayerCharacter::Move(const FInputActionInstance& Instance)
{
FVector2D MovementVector = Instance.GetValue().Get<FVector2D>();
if (Controller != nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector FowardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(FowardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ABasePlayerCharacter::Look(const FInputActionInstance& Instance)
{
FVector2D LookVector = Instance.GetValue().Get<FVector2D>();
if (Controller != nullptr)
{
AddControllerYawInput(LookVector.X);
AddControllerPitchInput(LookVector.Y);
}
}
이전 포스트에서 앞뒤, 좌우 이동과 상하, 좌우 카메라회전 총 네개의 함수를 구현했던 것과 달리 2차원 벡터를 받았기 때문에 두개의 함수로 구현할 수 있다.
컴파일하고 액션 매핑과 컨텍스트를 할당해주고 실행해서 확인해 본다.
게임 플레이를 누르고 `키를 눌러 콘솔창에 'showdebug enhancedinput'을 입력하면 디버깅 화면을 볼 수 있다.
좌측 패널에 바인딩 된 키들이 나오고 키를 누르면 정보들이 업데이트 되는 것을 확인할 수 있다.
'언리얼 엔진 > 기초' 카테고리의 다른 글
[Unreal Engine5] Inverse Kinematics (0) | 2023.07.29 |
---|---|
[Unreal Engine5] Anim Instance 클래스로 걷기/점프 애니메이션 구현(2) (0) | 2023.07.23 |
[Unreal Engine5] Anim Instance 클래스로 걷기/점프 애니메이션 구현(1) (0) | 2023.07.23 |
[Unreal Engine5] 모듈 추가하기 (0) | 2023.07.08 |
[Unreal Engine5] 캐릭터 카메라 설정과 캐릭터 움직이기 (0) | 2023.07.07 |