Notice
Recent Posts
Recent Comments
Link
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

UnrealEngineer

[ UnrealEngine5 ] 입력 시스템 설정(Inhanced Input) 본문

언리얼엔진5

[ UnrealEngine5 ] 입력 시스템 설정(Inhanced Input)

UnrealEngineer 2024. 8. 14. 09:30

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)로 설정