스토리지

Character 본문

Unreal Engine/0. Unreal 5

Character

ljw4104 2023. 7. 13. 18:15

1. Character Class

  • Actor > Pawn > Character 단위로 상속받으며 인간(캐릭터)에게 유용한 기능들이 많이 있다.
  • 왠만한 것들은 Pawn과 동일하다. ACharacter가 상속되어있다.

2. 기본 Component

Capsule Component

  • Mesh Component : 캐릭터의 메시
  • Arrow Component : 캐릭터의 전방 벡터를 가리키는 용도

Character Movement Component : 캐릭터 움직임에 관한 모든 것이 담겨져있음.

 

3. 캐릭터 움직이기

Pawn에서 했던 것처럼 마우스 X축 값에 따라 루트 컴포넌트(여기서는 Capsule Component)를 회전 시키면 캐릭터도 회전하기 때문에 사용하면 안된다.

여기서는 Spring Arm(Camera Boom)만 회전해서 그 전방 방향으로 캐릭터를 회전하는 방식을 사용해야 한다.

 

void ASlashCharacter::MoveForward(float Value)
{
	if (Controller && Value)
	{
		// Find out which way is forward
		const FRotator ControlRotation = GetControlRotation();
		const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);

		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}
  1. 컨트롤러의 회전값을 가져옴
  2. 그 회전값의 Yaw 값만 따로 꺼내서 FRotator 생성
  3. 해당 각의 회전행렬을 구해서 X축을 기준으로 전방벡터를 가져옴 (X축이 전방벡터이기 때문)
  4. 똑같이 AddMovementInput 함수에 방향과 정도를 넣는다.

회전 속도는

블루프린트에서는 해당 옵션에서 변경할 수 있고 코드에서는 다음과 같이 변경할 수 있다.

GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);
  • CharacterMovementComponent를 가져올 때는 GetCharacterMovement() 함수를 사용하면 해당 컴포넌트의 포인터가 반환된다.

좌우 움직임은 위의 코드와 모든 것이 동일하고 회전행렬에서 벡터를 꺼낼 때 Y축으로 꺼낸다는 차이가 있다 (좌우이기 때문)

void ASlashCharacter::MoveRight(float Value)
{
	if (Controller && Value)
	{
		// Find out which way is forward
		const FRotator ControlRotation = GetControlRotation();
		const FRotator YawRotation(0.f, ControlRotation.Yaw, 0.f);

		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}
}

'Unreal Engine > 0. Unreal 5' 카테고리의 다른 글

애니메이션 적용  (0) 2023.07.14
언리얼 Inverse Kinematic  (0) 2023.07.14
카메라 추가  (0) 2023.07.12
Pawn 움직이기  (0) 2023.07.12
Forward Declaration (전방 선언)  (0) 2023.07.12
Comments