Unreal 공부/Unreal Engine 4 C++ The Ultimate Shooter

[UE] C++에서 SpringArm과 Camera 구현하기

메카인 2023. 12. 22. 12:37

우선 헤더에 전방 선언을통해 포인터 변수를 생성해주고 ,Getter 함수를 UE INLINE 매크로로 선언해준다.

※ 전방 선언이란?
include를 하면 코드의 양이 증가하는데, 헤더에서 변수 선언시 구현 부분(멤버 함수 및 변수)이 필요 없는 경우에는 [class USpringArmComponent] 처럼 사용하여 구현부의 링킹을 미룬다. 주로 CPP파일에서 구현 부분을 사용하기 때문에 헤더의 크기를 줄이는 기법이다. (하나의 헤더를 여러 cpp이 include할 때 필요한 구현 부분만 cpp가 include 할 수 있어 코드의 양을 줄여준다.)
// ShooterCharacter.h
class UCameraComponent;
class USpringArmComponent;

...

public:
	FORCEINLINE USpringArmComponent *GetCameraBoom() const { return CameraBoom; };

	FORCEINLINE UCameraComponent *GetFollowCamera() const { return FollowCamera; };

private:
	// CameraBoom은 카메라를 캐릭터 뒷쪽에 위치시킵니다.
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	USpringArmComponent *CameraBoom;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	UCameraComponent *FollowCamera;

 

 

CPP 파일에서 필요한 부분을 include하고,  생성자에서 CameraBoom과 FollowCamera의 생성과 설정을 한다.

// ShooterCharacter.cpp
AShooterCharacter::AShooterCharacter()
{
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Create a CameraBoom (pulls in towards the character if there is a collision)
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);
	CameraBoom->TargetArmLength = 300.f;		// The camera follows at this distance behinde the character
	CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller

	// Create a follow camera
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach camera to end of CameraBoom [const FName USpringArmComponent::SocketName(TEXT("SpringEndpoint"));]
	FollowCamera->bUsePawnControlRotation = false;								// Camera does not rotate relative to arm
}
  • CreateDefaultSubobject : 컴포넌트 또는 서브오브젝트를 생성하는 기능으로, 자식 클래스를 생성하고 부모 클래스를 반환할 수 있게 합니다.
  • SetupAttachment : 컴포넌트가 등록될 때 붙여질 원하는 부착 부모(Attach Parent)와 소켓 이름(SocketName)을 초기화합니다.
  • USpringArmComponent::SocketName : SpringArm의 종단점의 소켓 FName 변수
    • FName USpringArmComponent::SocketName(TEXT("SpringEndpoint"))
  • TargetArmLength : USpringArmComopnent가 가지는 SpringArm의 길이 변수
  • bUsePawnControlRotation : 컴포넌트가 폰(게임 내 캐릭터나 오브젝트)의 시야 또는 조종 회전을 따를지 여부를 결정합니다. 활성화되면 컴포넌트는 폰의 회전을 따라갑니다.

 

컴파일 후 구현된 BP_ShooterCharacter