언리얼엔진5 공부

언리얼엔진5 공부언리얼엔진 C++ 챕터2 - 3. 캐릭터 동작 구현과 입력 처리하기

Client Side 2025. 9. 24. 22:46

1. Character 클래스에 액션 바인딩 추가하기

 

1-1. PlayerController는 키나 마우스 입력을 감지하고 각 입력 액션을 활성화해줄 뿐이다. 그다음 캐릭터 클래스에서 어떤 함수가 호출될지를 바인딩해주어야 최종적으로 동작이 이루어진다.

 

정리

1) PlayerController는 SpartaInputMappingContext(IMC)를 활성화한다.

2) 해당 IMC에는 IA_Move, IA_Jump 등의 UInputAction들이 키보드/마우스와 맵핑되어있다.

3) 캐릭터가 SetupPlayerInputComponent() 함수를 통해 각 액션이 발생했을 때 어떤 함수를 실행할지를 동록해둔다.

이렇게 등록한 함수들이, 실제로 움직이거나 점프하는 등 캐릭터 동작을 수행한다.

 

 

1-2. 캐릭터에 액션 바인딩 추가하기

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "SpartaCharacter.generated.h"

class USpringArmComponent;
class UCameraComponent;
// Enhanced Input에서 액션 값을 받을 때 사용하는 구조체
struct FInputActionValue;

UCLASS()
class SPARTAPROJECT_API ASpartaCharacter : public ACharacter
{
		GENERATED_BODY()

public:
		ASpartaCharacter();

protected:
		UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera")
		USpringArmComponent* SpringArmComp;
		UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera")
		UCameraComponent* CameraComp;
	
		// 입력 바인딩을 처리할 함수
		virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
		
		// IA_Move와 IA_Jump 등을 처리할 함수 원형
    // Enhanced Input에서 액션 값은 FInputActionValue로 전달됩니다.
	  UFUNCTION()
		void Move(const FInputActionValue& value);
		UFUNCTION()
		void StartJump(const FInputActionValue& value);
		UFUNCTION()
		void StopJump(const FInputActionValue& value);
		UFUNCTION()
		void Look(const FInputActionValue& value);
		UFUNCTION()
		void StartSprint(const FInputActionValue& value);
		UFUNCTION()
		void StopSprint(const FInputActionValue& value);
};

SpartaCharacter.h

 

  • Struct FInputActionValue : Enhanced Input에서 액션 값 (축 이동값, 마우스 이동량 등)을 전달할 때 사용하는 구조체로, .IA에서 설정한 Value Type.

 

#include "SpartaCharacter.h"
#include "SpartaPlayerController.h"
#include "EnhancedInputComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"

ASpartaCharacter::ASpartaCharacter()
{
		PrimaryActorTick.bCanEverTick = false;

    SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
    SpringArmComp->SetupAttachment(RootComponent);
    SpringArmComp->TargetArmLength = 300.0f;
    SpringArmComp->bUsePawnControlRotation = true;

    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
    CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
    CameraComp->bUsePawnControlRotation = false;
}

void ASpartaCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // Enhanced InputComponent로 캐스팅
    if (UEnhancedInputComponent* EnhancedInput = Cast<UEnhancedInputComponent>(PlayerInputComponent))
    {
        // IA를 가져오기 위해 현재 소유 중인 Controller를 ASpartaPlayerController로 캐스팅
        if (ASpartaPlayerController* PlayerController = Cast<ASpartaPlayerController>(GetController()))
        {
            if (PlayerController->MoveAction)
            {
                // IA_Move 액션 키를 "키를 누르고 있는 동안" Move() 호출
                EnhancedInput->BindAction(
                    PlayerController->MoveAction,
                    ETriggerEvent::Triggered,
                    this,
                    &ASpartaCharacter::Move
                );
            }
            
            if (PlayerController->JumpAction)
            {
                // IA_Jump 액션 키를 "키를 누르고 있는 동안" StartJump() 호출
                EnhancedInput->BindAction(
                    PlayerController->JumpAction,
                    ETriggerEvent::Triggered,
                    this,
                    &ASpartaCharacter::StartJump
                );
                
                // IA_Jump 액션 키에서 "손을 뗀 순간" StopJump() 호출
                EnhancedInput->BindAction(
                    PlayerController->JumpAction,
                    ETriggerEvent::Completed,
                    this,
                    &ASpartaCharacter::StopJump
                );
            }
            
            if (PlayerController->LookAction)
            {
                // IA_Look 액션 마우스가 "움직일 때" Look() 호출
                EnhancedInput->BindAction(
                    PlayerController->LookAction,
                    ETriggerEvent::Triggered,
                    this,
                    &ASpartaCharacter::Look
                );
            }
            
            if (PlayerController->SprintAction)
            {
                // IA_Sprint 액션 키를 "누르고 있는 동안" StartSprint() 호출
                EnhancedInput->BindAction(
                    PlayerController->SprintAction,
                    ETriggerEvent::Triggered, 
                    this, 
                    &ASpartaCharacter::StartSprint
                );
                // IA_Sprint 액션 키에서 "손을 뗀 순간" StopSprint() 호출
                EnhancedInput->BindAction(
                    PlayerController->SprintAction, 
                    ETriggerEvent::Completed, 
                    this, 
                    &ASpartaCharacter::StopSprint
                );
            }    
        }
    }
}

void ASpartaCharacter::Move(const FInputActionValue& value)
{
}

void ASpartaCharacter::StartJump(const FInputActionValue& value)
{
}

void ASpartaCharacter::StopJump(const FInputActionValue& value)
{
}

void ASpartaCharacter::Look(const FInputActionValue& value)
{
}

void ASpartaCharacter::StartSprint(const FInputActionValue& value)
{
}

void ASpartaCharacter::StopSprint(const FInputActionValue& value)
{
}

SpartaCharacter.cpp

 

  • InputComponent를 구체적으로 EnhancedInputComponenet로 캐스팅하기
  • 현재 소유중인 Controller를 ASpartaPlayerController로 캐스팅하기 -> 스파르타컨트롤러 안에 내가 만든 IA들을 사용하기위해.
  • PlayerContoller에 선언한 액션들을 정의해주기.
    • EnhancedInput의 BindAction함수를 통해 바인딩해주기
  • BindAction 함수
        - 첫 번째 인자 : 어떤 UInputAction과 연결할지
        - 두 번째 인자 : 액션이 발생하는 트리거 이벤트 (Triggered, Ongoing, Completed 등).
        - 세 번째/네 번째 인자: 액션 발생 시 실행할 객체(`this`)와 함수 포인터.
  • &ASpartaCharacter::Move
    • 멤버함수 포인터
    • &는 여기서 "주소"를 의미하는데, Move 함수의 메모리 주소를 가리킨다.
    • C++에서 멤버 함수를 가리킬 때는 &클래스명::함수명 형식을 사용한다.

 

2. 함수 구현

 

1) Move함수

void ASpartaCharacter::Move(const FInputActionValue& value)
{
		// 컨트롤러가 있어야 방향 계산이 가능
    if (!Controller) return;

		// Value는 Axis2D로 설정된 IA_Move의 입력값 (WASD)을 담고 있음
    // 예) (X=1, Y=0) → 전진 / (X=-1, Y=0) → 후진 / (X=0, Y=1) → 오른쪽 / (X=0, Y=-1) → 왼쪽
    const FVector2D MoveInput = value.Get<FVector2D>();

    if (!FMath::IsNearlyZero(MoveInput.X))
    {
		    // 캐릭터가 바라보는 방향(정면)으로 X축 이동
        AddMovementInput(GetActorForwardVector(), MoveInput.X);
    }

    if (!FMath::IsNearlyZero(MoveInput.Y))
    {
			   // 캐릭터의 오른쪽 방향으로 Y축 이동
        AddMovementInput(GetActorRightVector(), MoveInput.Y);
    }
}
  • FInputActionValue::Get<FVector2D>()
        - IA_Move가 Axis2D로 설정되어 있으므로, 2차원 벡터 형태로 입력이 들어온다.
  • AddMovementInput(방향, 크기)
        - 첫 번째 파라미터: 월드 좌표 기준 이동 방향(Forward, Right 등)
        - 두 번째 파라미터: 이동 스케일(속도)
        - 내부적으로 CharacterMovementComponent가 이 요청을 받아 속도를 계산하고, 실제 이동을 구현한다.

 

2) Jump 함수

void ASpartaCharacter::StartJump(const FInputActionValue& value)
{
    // Jump 함수는 Character가 기본 제공
    if (value.Get<bool>())
    {
        Jump();
    }
}

void ASpartaCharacter::StopJump(const FInputActionValue& value)
{
    // StopJumping 함수도 Character가 기본 제공
    if (!value.Get<bool>())
    {
        StopJumping();
    }
}
  • value.Get<bool>()
        - Enhanced Input System에서 전달된 입력 값을 `bool`로 가져온다.
        - 이 값은 점프 키(예: 스페이스바)가 눌렸는지 여부를 나타낸다.
            - true: 키가 눌림.
            - false: 키가 눌리지 않음.
  • StopJumping() , Jump()
        - Character 클래스에서 기본 제공되는 함수로, 캐릭터가 점프를 하거나 멈추도록 만들어준다.

 

3) Look 함수

void ASpartaCharacter::Look(const FInputActionValue& value)
{
    // 마우스의 X, Y 움직임을 2D 축으로 가져옴
    FVector2D LookInput = value.Get<FVector2D>();

    // X는 좌우 회전 (Yaw), Y는 상하 회전 (Pitch)
    // 좌우 회전
    AddControllerYawInput(LookInput.X);
    // 상하 회전
    AddControllerPitchInput(LookInput.Y);
}