UnrealEngineer
[ UnrealEngine5 ] 입력 시스템 설정(Inhanced Input) 본문
Inhanced Input
1. InputMappingContetxt, InputAction 생성
IputMappingContext 생성 | InputAction 생성 |
2. TObjectPtr<class UInputAction> MoveAction
에디터에서 생성한 InputAction, InputMappingContext를 맵핑시킬 포인터 변수를 생성한다.
◈ #include "InputActionValue.h" [ Value를 전달하기 위해 추가 ]
#pragma once
#include "CoreMinimal.h"
#include "BCCharacterBase.h"
#include "InputActionValue.h"
#include "BCCharacterPlayer.generated.h"
UCLASS()
class BIKINICITY_API ABCCharacterPlayer : public ABCCharacterBase
{
GENERATED_BODY()
// Input
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UInputMappingContext> DefaultMappingContext;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UInputAction> JumpAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UInputAction> MoveAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UInputAction> LookAction;
public:
ABCCharacterPlayer();
protected:
// #include "InputActionValue.h"
void Move(const FInputActionValue& Value);
void Look(const FInputActionValue& Value);
public:
// Input Setting
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
3. 포인터 변수에 생성한 InputAction과 InputMappingContext의 경로를 설정하고 바인딩
// Fill out your copyright notice in the Description page of Project Settings.
#include "BCCharacterPlayer.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "InputMappingContext.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
ABCCharacterPlayer::ABCCharacterPlayer()
{
// 입력 설정
static ConstructorHelpers::FObjectFinder<UInputMappingContext> DefaultMappingContextRef(TEXT("/Script/EnhancedInput.InputMappingContext'/Game/BikiniCity/Input/IMC_BCPlayer.IMC_BCPlayer'"));
if (nullptr != DefaultMappingContextRef.Object)
{
DefaultMappingContext = DefaultMappingContextRef.Object;
}
static ConstructorHelpers::FObjectFinder<UInputAction> InputActionMoveRef(TEXT("/Script/EnhancedInput.InputAction'/Game/BikiniCity/Input/Action/IA_BCMove.IA_BCMove'"));
if (nullptr != InputActionMoveRef.Object)
{
MoveAction = InputActionMoveRef.Object;
}
static ConstructorHelpers::FObjectFinder<UInputAction> InputActionJumpRef(TEXT("/Script/EnhancedInput.InputAction'/Game/BikiniCity/Input/Action/IA_BCJump.IA_BCJump'"));
if (nullptr != InputActionJumpRef.Object)
{
JumpAction = InputActionJumpRef.Object;
}
static ConstructorHelpers::FObjectFinder<UInputAction> InputActionLookRef(TEXT("/Script/EnhancedInput.InputAction'/Game/BikiniCity/Input/Action/IA_BCLook.IA_BCLook'"));
if (nullptr != InputActionLookRef.Object)
{
LookAction = InputActionLookRef.Object;
}
}
void ABCCharacterPlayer::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ABCCharacterPlayer::Look(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void ABCCharacterPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
{
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ABCCharacterPlayer::Move);
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ABCCharacterPlayer::Look);
}
}
4. InputMappingContext와 InputAction 내부 설정
IA_BCMove, IA_BCLook = Axis2D로 설정
IA_BCJump = Digital(bool)로 설정
'언리얼엔진5' 카테고리의 다른 글
[ UnrealEngine5 ] 액터의 리플리케이션 (0) | 2024.08.23 |
---|---|
[ UnrealEngine5 ] 연결과 오너 (0) | 2024.08.21 |
[ UnrealEngine5 ] 게임모드와 로그인 (0) | 2024.08.19 |
[ UnrealEngine5 ] 캐릭터 애니메이션 (0) | 2024.08.19 |
[ UnrealEngine5 ] 액터의 네트워크 역할 (0) | 2024.08.12 |