본문 바로가기

Unreal/언리얼 엔진 5로 개발하는 멀티플레이어 게임(Book)

[Unreal] US_Character.cpp 액션 코드 분석(입력)

US_Character.cpp

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

    if (Controller != nullptr)
    {
        const auto Rotation = Controller->GetControlRotation();
        const FRotator YawRotation(0, Rotation.Yaw, 0);

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

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

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

    if (Controller != nullptr)
    {
        AddControllerYawInput(LookAxisVector.X);
        AddControllerPitchInput(LookAxisVector.Y);
    }
}

void AUS_Character::SprintStart(const FInputActionValue& Value)
{
    GEngine->AddOnScreenDebugMessage(2, 5.f, FColor::Blue, TEXT("SprintStart"));
    GetCharacterMovement()->MaxWalkSpeed = 3000.f;
}

void AUS_Character::SprintEnd(const FInputActionValue& Value)
{
    GEngine->AddOnScreenDebugMessage(2, 5.f, FColor::Blue, TEXT("SprintEnd"));
    GetCharacterMovement()->MaxWalkSpeed = 500.f;
}

void AUS_Character::Interact(const FInputActionValue& Value)
{
    GEngine->AddOnScreenDebugMessage(3, 5.f, FColor::Red, TEXT("Interact"));
}

 

 

[UE5] Quat(쿼터니언) / Rotator (로테이터)

FQuat    쿼터니언은 3차원 공간에서 회전을 표현할 때 사용하는 수학적 도구이다. 쿼터니언의 장점     1. Gimbal Lock 방지 : 오일러 각도를 사용하여 각을 표현할 때  생기는 짐벌락을 방지할

plug-in-baby.tistory.com

 

언리얼의 오일러 각과 쿼터니언을 먼저 어떻게 쓰는지 알아 본 후 코드를 보면 좋다.

Move

  • FRotator: 오일러 각 회전을 지원한다. 메개변수로 Pitch, Yaw, Roll을 받는다
  • FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X): 전방 방향 벡터를 계산한다.
  • FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y): 우측 방향 벡터를 계산한다.
  • AddMovementInput(): 계산된 방향과 입력 값을 사용하여 캐릭터에 이동 입력을 추가한다. 해당 함수는 Pawn에 상속을 받았다.

Look

  • Value.Get<FVector2D>(): 시점 조작 입력을 2D 벡터로 받아온다.
  • AddControllerYawInput(LookAxisVector.X): X축 입력을 사용하여 좌우 회전(Yaw)을 적용한다.
  • AddControllerPitchInput(LookAxisVector.Y): Y축 입력을 사용하여 상하 회전(Pitch)을 적용한다.

SprintStart, SprintEnd, Interact

  • AddOnScreenDebugMessage: 화면에 디버깅 메시지를 뛰어준다.
  • GetCharacterMovement()->MaxWalkSpeed: character에서 상속 받은 함수로 이동 속도를 정한다.