TIL: Unreal C++ 42일차

박춘팔·어제

언리얼 TIL

목록 보기
41/41

누적 학습 시간 : 442시간 34분

📅 2026-06-02

C++ 배치고사와 커리어데이

캐릭터 이동

// Copyright Epic Games, Inc. All Rights Reserved.

#include "BCLifePlayableCharacter.h"
#include "Engine/World.h"

#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/CapsuleComponent.h"

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

	// 1인칭에서는 캐릭터 몸의 Yaw 회전이 컨트롤러를 따라가야함
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = true;

	GetCharacterMovement()->bOrientRotationToMovement = true;
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
	
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(GetCapsuleComponent());
	FollowCamera->SetRelativeLocation(FVector(0.f, 0.f, 64.f));
	FollowCamera->bUsePawnControlRotation = true;

	ApplyMovementSettings();
}

void ABCLifePlayableCharacter::BeginPlay()
{
	Super::BeginPlay();
	ApplyMovementSettings();
}

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

	// 복잡한 Enhanced Input Asset 없이도 바로 움직일 수 있도록 기본 Axis Mapping에 연결합니다.
	PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ABCLifePlayableCharacter::MoveForward);
	PlayerInputComponent->BindAxis(TEXT("MoveRight"), this, &ABCLifePlayableCharacter::MoveRight);
	PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ABCLifePlayableCharacter::Turn);
	PlayerInputComponent->BindAxis(TEXT("LookUp"), this, &ABCLifePlayableCharacter::LookUp);
}

void ABCLifePlayableCharacter::MoveForward(float Value)
{
	if (!Controller || FMath::IsNearlyZero(Value))
	{
		return;
	}

	const FRotator YawRotation(0.0f, Controller->GetControlRotation().Yaw, 0.0f);
	AddMovementInput(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X), Value);
}

void ABCLifePlayableCharacter::MoveRight(float Value)
{
	if (!Controller || FMath::IsNearlyZero(Value))
	{
		return;
	}

	const FRotator YawRotation(0.0f, Controller->GetControlRotation().Yaw, 0.0f);
	AddMovementInput(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y), Value);
}

void ABCLifePlayableCharacter::Turn(float Value)
{
	AddControllerYawInput(Value);
}

void ABCLifePlayableCharacter::LookUp(float Value)
{
	AddControllerPitchInput(Value);
}

void ABCLifePlayableCharacter::ApplyMovementSettings()
{
	if (UCharacterMovementComponent* Movement = GetCharacterMovement())
	{
		Movement->MaxWalkSpeed = WalkSpeed;
	}
}
profile
이것 저것 다해보는 삶

0개의 댓글