Unreal/언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)
[Unreal] US_Character.cpp 생성자 코드 분석(카메라)
suppresswisely
2025. 3. 22. 15:48
US_Character.cpp
#include "US_Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
AUS_Character::AUS_Character()
{
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 800.0f;
CameraBoom->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
FollowCamera->bUsePawnControlRotation = false;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
GetCapsuleComponent()->InitCapsuleSize(60.f, 96.0f);
GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -91.f));
static ConstructorHelpers::FObjectFinder<USkeletalMesh> SkeletalMeshAsset(TEXT("/Game/KayKit/Characters/rogue"));
if (SkeletalMeshAsset.Succeeded())
{
GetMesh()->SetSkeletalMesh(SkeletalMeshAsset.Object);
}
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f);
GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
}
CameraBoom
- CreateDefaultSubobject<T>: 초기화를 위해 사용한다. 즉, 변수 안에 CameraBoom에 Character의 USpringArmComponent 컴포넌트가 들어간다.
- SetupAttachment(): 부모-자식 관계를 만들기 위해 사용한다. 코드에서는 RootComponent의 자식으로 CameraBoom이 들어간다.
- RootComponent: 해당 변수에 선언된 곳을 보면 Actor.h에 존재한다. 그 위치에 UPROPERTY를 보면 Category가 Transformation으로 되어 있다. 즉, RootComponent는 Transformation이라 해도 틀리지 않은 것 같다.
- TargetArmLength: 카메라 거리 조절을 위한 변수다.
FollowCamera
- SetupAttachment(): 여기서 사용할 때 매개변수로 USpringArmComponent::SocketName이 있는데, 이는 부모-자식 관계를 만들 때 옵션을 추가하는 역할을 한다. 여기서는 SpringArm의 끝쪽에 놓아 달라는 역할을 하고 있다. SocketName에 대해 더 조사하면, 아무 컴포넌트나 가지고 있는 것이 아니기에 항상 사용하는 것은 아니다.
Camera Rotation
- CameraBoom->bUsePawnControlRotation: CameraBoom(Spring Arm)이 플레이어의 컨트롤러 회전을 따르도록 설정
- FollowCamera->bUsePawnControlRotation: 카메라 자체는 컨트롤러의 회전을 직접 따르지 않도록 설정
- bUseControllerRotation: 캐릭터 자체가 컨트롤러의 회전을 따르지 않도록 설정
GetCapsuleComponent()->InitCapsuleSize(60.f, 96.0f)
Capsule 컴포넌트의 크기를 조절하는 것이 한눈에 봐도 알 수 있다. 다만 이 함수로 알 수 있는 것은 Character를 상속받으면 무조건 CapsuleComponent가 붙는다는 것을 간접적으로 알게 된다.
GetMesh()->SetRelativeLocation(FVector(0.f, 0.f, -91.f))
Mesh 컴포넌트의 위치를 변경한다. 언리얼에서는 벡터를 다룰 때 FVector로 사용한다는 것을 간접적으로 알 수 있다.
ConstructorHelpers::FObjectFinder<USkeletalMesh>
3D 물체를 가져오기 위해 사용한다. 이전 글에서도 언급했지만 ConstructorHelpers에서 FObject를 사용한다는 것은 실질적으로 애셋을 쓰기 위해 메모리에 로드한 것을 사용한다는 의미다.
GetCharacterMovement
- bOrientRotationToMovement: 캐릭터가 이동하는 방향으로 자동으로 회전하도록 설정
- RotationRate: 캐릭터의 회전 속도를 설정
- MaxWalkSpeed: 캐릭터의 최대 걷기 속도를 설정
- MinAnalogWalkSpeed: 아날로그 입력(예: 조이스틱)을 사용할 때의 최소 걷기 속도를 설정
- BrakingDecelerationWalking: 캐릭터가 얼마나 빨리 멈추는지를 결정
코드를 작성한 후 블루프린트를 보면 생성자에 작성된 코드가 실제로 표시된다. 이를 통해 언리얼의 생성자가 꽤나 강력하고 중요하다는 것을 알 수 있다.