UE4

[UE4] GTA 방식의 카메라 구현

Honey Badger 2022. 10. 5. 21:34

Character 클래스에 함수를 작성하고, 스프링암을 활용해서 GTA 방식의 삼인칭 조작에 관련된 기능을 설정해보자.

ControlRotation(컨트롤 회전)

   PlayerController는 플레이어의 의지를 나타내는 ControlRotation이라는 속성을 제공한다. 플레이어의 입력값을 컨트롤 회전에 연동하는 방법을 알아보자. 일단 프로젝트 세팅의 입력설정에서 원하는 키를 설정한 후 AddControllerInputYaw, Roll, Pitch라는 세가지 명령을 사용하면 PlayerController의 ControlRotation값이 입력에 따라 변한다. 이러한 컨트롤 회전 값은 캐릭터의 카메라 설정에서 다양하게 사용된다. 

 

  플레이어 컨트롤러에서는 마우스 입력 값에 따라 변화하는 컨트롤 회전 값의 스케일을 지정할 수 있다. 내장된 속성인 Input Yaw, Pitch, Roll Scale 값을 변경하면 컨트롤 회전 값의 변화량이 달라진다.

 

Character와 ControlRotation

  UnrealEngine의 Character모델은 기본적으로 ControlRotation의 Yaw회전(Z축 회전) 값과 Pawn의 Yaw 회전이 서로 연동돼 있다. 이를 지정하는 속성이 액터의 Pawn 섹션에 위치한 UseControllerRotationYaw이다. 내가 생성한 캐릭터의 Pawn 섹션을 보면 UseControllerRotationYaw만 체크되어 있는 것을 알 수 있다. Pitch도 체크한다면 캐릭터는 마우스 Input에 따라 앞뒤로도 회전할 수 있다. 

 

GTA 방식의 삼인칭 컨트롤 구현

  SpringArm Component는 UnrealEngine에서 삼인칭 시점의 카메라 설정을 구현할 때 편리하게 사용할 수 있는 Component이다. SpringArm Component에도 마찬가지로 PlayerController의 ControlRotation 값을 활용하도록 여러 속성들이 설계되어 있다. 다음 코드를 통해 이를 알아보자.

SpringArm->TargetArmLength = 450.0f;
	SpringArm->SetRelativeRotation(FRotator::ZeroRotator);
	SpringArm->bUsePawnControlRotation = true;
	SpringArm->bInheritPitch = true;
	SpringArm->bInheritRoll = true;
	SpringArm->bInheritYaw = true;
	SpringArm->bDoCollisionTest = true;
	bUseControllerRotationYaw = false;
	GetCharacterMovement()->bOrientRotationToMovement = true;
	GetCharacterMovement()->bUseControllerDesiredRotation = false;
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 720.0f, 0.0f);

 

1. TargetArmLength

  - SpringArm의 길이(Camera 섹션에 있음.)

 

2. SetRelativeRotation()

  - SpringArm 회전 (ZeroRotator인걸 보니 초기값 설정.)

 

3. bUsePawnControlRotation

  - 부모의 ControlRotation값을 사용할지.

 

4. bInheritPitch, Roll, Yaw

  - 부모의 Pitch, Roll, Yaw값을 상속받을지.

 

5. bDoCollisionTest

  - 카메라 CollisionTest를 할지. (안하면 물체들에 가려 플레이어가 안보일 수 있음.)

 

6. bUseControllerRotationYaw

  - 위의 설명처럼 연동되어 있던 Yaw값 false처리. (더이상 마우스 인풋에 캐릭터가 회전X)

 

7. CharacterMovement Component 

  - bOrientRotationToMovement : 캐릭터가 움직이는 방향으로 캐릭터를 자동 회전시켜주는 기능.

  - bUseControllerDesiredRotation : 컨트롤 회전이 가리키는 방향으로 캐릭터가 부드럽게 회전.

  - RotationRate : 초당 얼마만큼 변화할지, UseControllerDesiredRotation나 OrientRotationToMovement가 true일때 사용.