Unreal5 캐릭터 회전 C++

Unreal5 캐릭터 회전 C++.

*이 글은 기록용이며, 이 글만 봐서는 무슨말을 하는지 모를 수 있습니다.

오늘은 저번에 만들던거 이어서 만들어보려고 합니다.

제목은 캐릭터 회전인데 사실 카메라 회전이 더 맞는말이네요.

[프로그래밍/언리얼] - Unreal5 이동 애니메이션

 

Unreal5 이동 애니메이션

Unreal5 이동 애니메이션. 안녕하세요. 오늘은 Idle상태와 Run상태의 애니메이션을 추가해보겠습니다. 이전에 작성한 글에서 이어서 작성하도록 하겠습니다. [프로그래밍/언리얼] - Unreal5 Camera 와 Spr

intunknown.tistory.com

  • Enhanced Input 추가 및 함수 작성.
  • 중간점검.
  • 수정.

Enhanced Input 추가 및 함수 작성.

내용 자체는 전에 했던 것들과 비슷비슷 합니다.

input Action을 하나 만들고 이름은 IA_Looking으로 지었습니다.

Value Type은 vector2D입니다.

SlayerCharacter.h파일의 protected에 다음 코드를 추가합니다.

UPROPERTY(EditAnywhere, Category = Input)
UInputAction* LookAction;
void Look(const FInputActionValue& value);

회전만 하는거라 헤더파일에 작성할게 별로 없네요.

SlayerCharacter.cpp에 다음 코드를 추가합니다.

X값으로 Yaw를 Controll하고, Y값으로 Pitch를 컨트롤 하는 코드입니다.

void ASlayerCharacter::Look(const FInputActionValue& value)
{
	const FVector2D LookVector = value.Get<FVector2D>();
	AddControllerYawInput(LookVector.X);//도리도리
	AddControllerPitchInput(LookVector.Y);//끄덕끄덕
}

함수도 추가했으니 바인드 해주는 코드도 아래 추가해줍니다.

그냥 대충 이런식으로 추가한 Look함수를 바인드 해줍니다.

// Called to bind functionality to input
void ASlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	//#include "EnhancedInputComponent.h"추가.
	if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
		EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Triggered, this, &ASlayerCharacter::Move);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ASlayerCharacter::Jump);
		EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASlayerCharacter::Look);
	}
}

함수작성은 다 했으니 일단 기쁜마음으로 실행해봅시다.

InputAction연결하고

Input Mapping Context에 연결합니다.

중간점검.

이게 끝이 아니고 중간점검인 이유는 제 생각대로 되지 않았기 때문입니다.

Rotate관련 설정들좀 대충 만지고 시작하니까 캐릭터가 돌아가더라고요...

좌우로는 괜찮은데 위아래로 하면 캐릭터가 엎드립니다. 저거 바닥입니다.

공중부양 능력자.

수정.

그래서 캐릭터 회전을 잠그고, 카메라를 회전시킬 생각입니다.

그래서 SlayerCharacter의 생성자를 아래 처럼 수정했습니다.

Spring Arm과 Character Rotate부분을 수정했습니다.

ASlayerCharacter::ASlayerCharacter()
{
 	// 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;
	/*spring arm*/
	//#include "GameFramework/SpringArmComponent.h"필요
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(GetRootComponent());
	CameraBoom->TargetArmLength = 300.f;
	CameraBoom->bUsePawnControlRotation = true;
	/*CharacterRotate*/
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = false;
	//#include "GameFramework/CharacterMovementComponent.h" 추가
	GetCharacterMovement()->bOrientRotationToMovement = true;
	/*Camera*/
	ViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ViewCamera"));
	ViewCamera->SetupAttachment(CameraBoom);
}

잘 돌아가네요.

 

Designed by JB FACTORY