< [언리얼엔진5 Part2 복습 / 개인 프로젝트] 2. 캐릭터와 입력시스템

언리얼엔진5/[Part2 복습] 이득우의 언리얼 프로그래밍

[언리얼엔진5 Part2 복습 / 개인 프로젝트] 2. 캐릭터와 입력시스템

Rocketbabydolls 2024. 4. 18. 16:05

캐릭터와 입력시스템 

 

1인칭 기본 템플릿과 이득우 님의 강의 코드를 기반으로 캐릭터 이동 및 카메라 회전까지 구현하였다. 

코드를 복습하면서 2번 브랜치에 주석을 달아 놓았다.

 

https://github.com/chataeg/GunBattle/tree/2

 

GitHub - chataeg/GunBattle

Contribute to chataeg/GunBattle development by creating an account on GitHub.

github.com

 

 

이해하기 가장 애먹었던 부분은 Move 함수였다. 

 

직접 만든 Move 함수

 

void AGBCharacterPlayer::Move(const FInputActionValue& Value)
{
	FVector2D MovementVector = Value.Get<FVector2D>();

	const FRotator Rotation = Controller->GetControlRotation();
	const FRotator YawRotation(0, Rotation.Yaw, 0);

	const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
	const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

	AddMovementInput(ForwardDirection, MovementVector.Y);
	AddMovementInput(RightDirection, MovementVector.X);
	
}

 

FVector2D MovementVector = Value.Get<FVector2D>();

 

->  z축을 제외한 x,y 축 이동 벡터를 가져옴

 

const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);

 

-> 컨트롤러에서 회전값을 가져와서 pitch, roll 값 제거

 

const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

 

-> 회전값을 가지고 있는 행렬에서 X축값, Y축값을 빼낸다.

  ※ 주의 : 행렬의 X축값과 언리얼 좌표계에서의 X축은 다르다.

    Y축 값 = 앞 or 뒤 ( 1.0 or -1.0 )  

    X축 값 = 오른쪽 or 왼쪽 ( 1.0 or -1.0 )  

 

AddMovementInput(ForwardDirection, MovementVector.X);
AddMovementInput(RightDirection, MovementVector.Y);

 

-> 추출한 방향을 토대로 벡터 크기만큼 이동

 

 

하지만 위의 코딩은 전부 쓸모없는 짓이었다.
간단히 구현이 가능했기 때문.

 

템플릿  Move 함수

void AGBCharacterPlayer::Move(const FInputActionValue& Value)
{
	FVector2D MovementVector = Value.Get<FVector2D>(); // z축을 제외한 x,y 축 이동 벡터를 가져옴

	if (Controller != nullptr)
	{
		// add movement 
		AddMovementInput(GetActorForwardVector(), MovementVector.Y);
		AddMovementInput(GetActorRightVector(), MovementVector.X);
	}
	
}

 

간단히 메서드를 통해 앞, 오른쪽의 벡터값을 가져올 수 있었다. . . .

사실 결과적으로는 같은 동작을 하지만 나는 좀 더 세부적으로 풀어 쓴 게 되었다.

 

주의해야 할 점은

 

 

 

캐릭터가 카메라의 회전을 따라서 회전하게 만들어주어야 한다는 점이다. (1인칭 한정, 3인칭에선 카메라 회전을 시키면 안 된다.)

 

 

Look 함수

 

void AGBCharacterPlayer::Look(const FInputActionValue& Value)
{
	
	FVector2D LookAxisVector = Value.Get<FVector2D>();

	AddControllerYawInput(LookAxisVector.X);  
	AddControllerPitchInput(LookAxisVector.Y);  

}

 

Roll 이 없는 이유는 고개가 좌우를 볼때 화면이 회전하지 고개가 돌아가지 않기 때문이다.

간단히 생각해보면 시선의 전환은 상하 좌우로 이루어지는데, 

    상하 -> Ptich

    좌우 -> Yaw 

이다.

 

   Roll ->  몸이 회전함 ???? 

 

고로 Roll은 추가해주지 않는다. 

 

 

여기까지 캐릭터 회전 및 입력/이동까지 완성했다.

 

 

 

실행 영상