Unreal - 캐릭터 기본 인풋 설정하기

2025. 8. 8. 00:35·Unreal 프로젝트 다이어리/두번째 프로젝트

3인칭 백뷰 시점의 캐릭터를 만들어주었습니다

필요한 헤더 선언

 

UPROPERTY(EditAnywhere, Category="Camera")
class USpringArmComponent* cameraBoom;

UPROPERTY(EditAnywhere, Category = "Camera")
class UCameraComponent* mainCamera;

 

AMainCharacter::AMainCharacter()
{
	PrimaryActorTick.bCanEverTick = true;
	SetRootComponent(GetCapsuleComponent());
	GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -90.f));
	GetMesh()->SetRelativeRotation(FRotator(0.f, -90.f, 0.f));
	//Character Settings
	ConstructorHelpers::FObjectFinder<USkeletalMesh> mainCharAsset
	(TEXT("/Game/MainCharacter/SKM_MainCharacter.SKM_MainCharacter"));
	if (mainCharAsset.Succeeded())
	{
		GetMesh()->SetSkeletalMesh(mainCharAsset.Object);
	}

	cameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("MainCameraBoom"));
	cameraBoom->SetupAttachment(RootComponent);

	//view settings (BackView, thirdPerson)
	cameraBoom->TargetArmLength = 400.f;
	cameraBoom->SetRelativeRotation(FRotator(0.f));
	cameraBoom->bEnableCameraLag = true;
	cameraBoom->CameraLagSpeed = 15.f;

	//camera hit handling
	cameraBoom->bDoCollisionTest = true;

	mainCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MainCamera"));
	mainCamera->SetupAttachment(cameraBoom);
	mainCamera->SetRelativeLocation(FVector(33.f, 0.f, 180.f));
	mainCamera->SetRelativeRotation(FRotator(-17.5f, 0.f, 0.f));
	mainCamera->bUsePawnControlRotation = false;

	//camera mouse handling
	bUseControllerRotationYaw = true;
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement()->bOrientRotationToMovement = false;

}

 

Movement코드를 짜주겠습니다

로직

W: 앞

A : 왼쪽

S : 뒤

D : 오른쪽

 

프로젝트 세팅에서 액션매핑을 설정후 코드를 작성해주었습니다

UCLASS()
class PORTFOLIOMS_API AMainCharacterController : public APlayerController
{
	GENERATED_BODY()
	
public:
	AMainCharacterController();

	virtual void OnPossess(APawn* mainCharacter) override;
	virtual void SetupInputComponent() override;
	virtual void Tick(float deltaTime) override;

	//Movement
	bool bMoveForward;
	bool bMoveBack;
	bool bMoveLeft;
	bool bMoveRight;

	void InputKeyboardWPressed() { bMoveForward = true; }
	void InputKeyboardWReleased() { bMoveForward = false; }
	void InputKeyboardAPressed() { bMoveLeft = true; }
	void InputKeyboardAReleased() { bMoveLeft = false; }
	void InputKeyboardSPressed() { bMoveBack = true; }
	void InputKeyboardSReleased() { bMoveBack = false; }
	void InputKeyboardDPressed() { bMoveRight = true; }
	void InputKeyboardDReleased() { bMoveRight = false; }

	UFUNCTION()
	void CharacterMovement(float deltaSeconds);
};

 

void AMainCharacterController::SetupInputComponent()
{
	Super::SetupInputComponent();

	InputComponent->BindAction("KeyboardW", IE_Pressed, this, &AMainCharacterController::InputKeyboardWPressed);
	InputComponent->BindAction("KeyboardW", IE_Released, this, &AMainCharacterController::InputKeyboardWReleased);
	InputComponent->BindAction("KeyboardA", IE_Pressed, this, &AMainCharacterController::InputKeyboardAPressed);
	InputComponent->BindAction("KeyboardA", IE_Released, this, &AMainCharacterController::InputKeyboardAReleased);
	InputComponent->BindAction("KeyboardS", IE_Pressed, this, &AMainCharacterController::InputKeyboardSPressed);
	InputComponent->BindAction("KeyboardS", IE_Released, this, &AMainCharacterController::InputKeyboardSReleased);
	InputComponent->BindAction("KeyboardD", IE_Pressed, this, &AMainCharacterController::InputKeyboardDPressed);
	InputComponent->BindAction("KeyboardD", IE_Released, this, &AMainCharacterController::InputKeyboardDReleased);
}
void AMainCharacterController::CharacterMovement(float deltaSeconds)
{
	APawn* myPawn = GetPawn();
	if (!myPawn) return;

	FRotator controlRot = GetControlRotation();
	controlRot.Pitch = 0.f; 
	controlRot.Roll = 0.f;

	FVector forwardDir = FRotationMatrix(controlRot).GetUnitAxis(EAxis::X);
	FVector rightDir = FRotationMatrix(controlRot).GetUnitAxis(EAxis::Y);

	FVector direction = FVector::ZeroVector;
	if (bMoveForward)
	{
		direction += forwardDir;
	}
	if (bMoveBack)
	{
		direction -= forwardDir;
	}	
	if (bMoveRight)
	{
		direction += rightDir;
	}	
	if (bMoveLeft)
	{
		direction -= rightDir;
	}
	if (!direction.IsNearlyZero())
	{
		direction.Normalize();
		myPawn->AddMovementInput(direction, 1.f);
	}
}

키보드 입력으로 간단하게 움직일수 있게 되었습니다

 

좌우로 마우스를 회전시 카메라를 시점을 변동시켜주겠습니다

인풋에 축매핑에 별도로 MouseX를 설정후 마우스x값 스케일 1.0으로 설정해준뒤 진행합니다

//mouse Axis
UFUNCTION()
void TurnCamera(float axisValue);

UPROPERTY(EditAnywhere, Category="Mouse")
float mouseSensitivity = 0.5;
InputComponent->BindAxis("MouseX", this, &AMainCharacterController::TurnCamera);
void AMainCharacterController::TurnCamera(float axisValue)
{
	AddYawInput(axisValue * mouseSensitivity);
}

 

자유롭게 시점이 좌우 회전이 가능해졌습니다

'Unreal 프로젝트 다이어리 > 두번째 프로젝트' 카테고리의 다른 글

Unreal - 무기 Draw  (0) 2025.08.19
Unreal - Layered Per Bone (상 하체 애니메이션 분리)  (0) 2025.08.17
Unreal - Foot IK  (0) 2025.08.17
Unreal - 모션매칭(Motion Matching) - 스키마 데이터 관리하기 (점프,달리기)  (0) 2025.08.15
Unreal - 모션매칭(Motion Matching) - 기본이동 구현하기  (0) 2025.08.09
'Unreal 프로젝트 다이어리/두번째 프로젝트' 카테고리의 다른 글
  • Unreal - Layered Per Bone (상 하체 애니메이션 분리)
  • Unreal - Foot IK
  • Unreal - 모션매칭(Motion Matching) - 스키마 데이터 관리하기 (점프,달리기)
  • Unreal - 모션매칭(Motion Matching) - 기본이동 구현하기
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (211) N
      • Unreal 프로젝트 다이어리 (108) N
        • 첫번째 프로젝트 (73)
        • 두번째 프로젝트 (35) N
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • C++ 프로그래머스 (52)
        • Stack,Queue (7)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (7)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (14)
      • C++ 백준 (4)
      • C++ 팁 (1)
      • 개인 코테 & 스타디 <비공개> (29)
        • 코드 개인보관함 (9)
        • 코딩테스트+@ (11)
        • 알고리즘 스타디 (6)
        • 알고리즘 스타디 과제 (3)
        • 비공개 (0)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    언리얼 시퀀스
    unreal 모션매칭
    unreal 인벤토리
    언리얼 모션매칭
    unreal inventory
    언리얼 behavior tree
    unreal 시퀀스
    언리얼
    언리얼 컷씬
    Unreal Parkour
    언리얼 프로그래스바
    언리얼 ui
    언리얼 motionmatching
    언리얼 behaviortree
    언리얼 parkour
    unreal 파쿠르
    언리얼 상호작용
    언리얼 파쿠르
    언리얼 인벤토리
    언리얼 비헤이비어트리
  • hELLO· Designed By정상우.v4.10.3
lucodev
Unreal - 캐릭터 기본 인풋 설정하기
상단으로

티스토리툴바