1. 캐릭터 클래스에서 입력 액션 연결의 개념
PlayerController는 키나 마우스 입력을 감지하고, 각 IA를 활성화해 줄 뿐이다.
그 다음 캐릭터 클래스에서 "어떤 함수가 호출될지"를 바인딩해 주어야 최종적으로 동작이 이루어진다.
- PlayerController는 IMC를 활성화한다.
- 해당 IMC에는 UInputAction들이 키보드/마우스와 맵핑되어 있다.
- 캐릭터는 SetupPlayerInputComponent() 함수를 통해 "각 액션이 발생했을 때 어떤 함수를 실행할지"를 등록한다.
- 이렇게 등록한 함수들이, 실제로 움직이거나 점프하는 등 캐릭터 동작을 수행한다.
2. 캐릭터에 액션 바인딩 추가하기
Character.h
- FInputActionValue를 전방 선언한다.
- IA_Move와 IA_Jump 등을 처리할 함수 원형을 선언한다.
- Enhanced Input에서 액션 값은 FInputActionValue로 전달된다.
Character.cpp
- EnhancedInputComponent를 사용하기 위해 Include 한다.
void AProjectCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInput = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
if (AProjectPlayerController* PlayerController = Cast<AProjectPlayerController>(GetController()))
{
if (PlayerController->MoveAction)
{
EnhancedInput->BindAction(
PlayerController->MoveAction,
ETriggerEvent::Triggered,
this,
&AProjectCharacter::Move
);
}
if (PlayerController->JumpAction)
{
EnhancedInput->BindAction(
PlayerController->JumpAction,
ETriggerEvent::Triggered,
this,
&AProjectCharacter::StartJump
);
EnhancedInput->BindAction(
PlayerController->JumpAction,
ETriggerEvent::Completed,
this,
&AProjectCharacter::StopJump
);
}
if (PlayerController->LookAction)
{
EnhancedInput->BindAction(
PlayerController->LookAction,
ETriggerEvent::Triggered,
this,
&AProjectCharacter::Look
);
}
if (PlayerController->SprintAction)
{
EnhancedInput->BindAction(
PlayerController->SprintAction,
ETriggerEvent::Triggered,
this,
&AProjectCharacter::StartSprint
);
EnhancedInput->BindAction(
PlayerController->SprintAction,
ETriggerEvent::Completed,
this,
&AProjectCharacter::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)
{
}
- 함수들을 바인딩 한다.
FinputActionValue
- Enhanced Input에서 액션 값(축 이동값, 마우스 이동량 등)을 전달할 때 사용하는 구조체로, IA에서 설정한 Value Type이다.
UFUNCTION()
- 입력 바인딩 함수는 언리얼 엔진 리플렉션 시스템과 연동되어야 한다.
- UFUNCTION()을 붙이지 않으면 바인딩에 실패할 수 있다.
- 블루프린트 접근성을 설정하지 않았더라도, 기본적으로 메타데이터가 생성된다.
- 언리얼 엔진의 입력 처리 시스템은 바인딩된 함수가 리플렉션 시스템을 통해 접근 가능한지 확인한다.
BindAction 함수
- 첫번째 인자: 어떤 UInputAction과 연결할지 (예: MoveAction)
- 두번째 인자: 액션이 발생하는 트리거 이벤트(Triggered, Ongoing, Completed) 등
- 세번째/네번째 인자: 액션 발생 시 실행할 객체(this)와 함수 포인터
점프와 스프린트 함수 분리
- "키를 누를 때"와 "뗄 때"가 다르게 처리될 수 있으므로 두 함수로 분리했다.
3. Move 함수 구현하기
FInputActionValue::Get<FVector2D>()
- IA_Move가 Axis2D로 설정되어 있으므로, 2차원 벡터 형태로 입력이 들어온다.
- W/S/D/A를 동시에 누를 수도 있으므로 (1, 1) 같은 형태도 가능하다.
AddMovementInput(방향, 크기)
- 첫번째 파라미터: 월드 좌표 기준 이동 방향(Forward, Right 등)
- 두번째 파라미터: 이동 스케일(속도)
- 내부적으로 CharacterMovementComponent가 이 요청을 받아 속도를 계산하고, 실제 이동을 구현한다.
4. Start Jump, Stop Jump 구현하기
value.Get<bool>()
- Enhanced Input System에서 전달된 입력 값을 bool로 가져온다.
- 이 값은 점프 키가 눌렸는지 여부를 나타낸다.
StopJumping(), Jump()
- Character 클래스에서 기본 제공되는 함수로, 캐릭터가 점프를 하거나 멈추도록 만들어 준다.
5. Look 함수 구현하기
AddControllerYawInput()
- 카메라의 Yaw 축 (수평 회전)을 변경
AddControllerPitchInput()
- 카메라의 Pitch 축 (수직 회전)을 변경
실제 어느 방향으로 얼마나 회전할지는 프로젝트 세팅 > Input > Mouse Sensitivity나 LookInput에 곱해줄 스케일(Modifier)을 통해 조정할 수 있다.
6. 스프린트 동작 구현하기
6-1. 스프린트 관련 멤버 변수 및 함수 설정하기
Character.h
- GetCharacterMovement()->MaxWalkSpeed를 사용할 때 필요한 CharacterMovementComponent를 선언해 준다.
- 이동 속도 관련 프로퍼티를 정의한다.
Character.cpp
- 생성자에서 멤버 변수들을 초기화해 준다.
GetCharacterMovement()
- ACharacter 클래스에 기본 내장된 함수로, UCharacterMovementComponent* 포인터를 반환한다.
- 여기서 MaxWalkSpeed 등의 이동 관련 설정을 바꿀 수 있다.
6. 결과
'Unreal' 카테고리의 다른 글
TSubclassOf vs TSoftClassPtr (0) | 2025.02.13 |
---|---|
Enhanced Input System의 이해 및 Input Action 설정하기 (0) | 2025.02.12 |
Player Controller (0) | 2025.02.12 |
Pawn과 Character 클래스 이해하기 (0) | 2025.02.06 |
GameMode 이해하기 (0) | 2025.02.06 |