애니메이션의 부자연스러움

GIF 2023-03-02 오후 7-35-12.gif

오른쪽 , 왼쪽 , 대각선 등으로 움직일때 이동이 뭔가 부자연스럽다 , 무작정 앞으로 걷는 애니메이션을 재생하기 때문이다.

Velocity , AimRotation 을 사용하자

void UShooterAnimInstance::UpdateAnimationProperties(float DeltaTime)
{
	if (nullptr == ShooterCharacter)
	{
		ShooterCharacter = Cast<AShooterCharacter>(TryGetPawnOwner());
	}
	if (ShooterCharacter)
	{
		// Get the lateral Speed of the character from velocity
		FVector Velocity{ ShooterCharacter->GetVelocity() };
		Velocity.Z = 0;
		Speed = Velocity.Size();

		// Is the character in the air?
		bIsInAir = ShooterCharacter->GetCharacterMovement()->IsFalling();

		// Is the character accelerating?
		// 가속도를 구해서 만약 가속이 0이라면 캐릭터는 움직이지 않는다고 판별한다.
		if (0 < ShooterCharacter->GetCharacterMovement()->GetCurrentAcceleration().Size())
			bIsAccelerating = true;
		else
			bIsAccelerating = false;

		FRotator AimRotation = ShooterCharacter->GetBaseAimRotation();
		FRotator MovementRotation = UKismetMathLibrary::MakeRotFromX(ShooterCharacter->GetVelocity());

		MovementOffsetYaw = UKismetMathLibrary::NormalizedDeltaRotator(MovementRotation, AimRotation).Yaw;

		/*
		FString RotationMessage = FString::Printf(TEXT("Base Aim Rotation : %f"), AimRotation.Yaw);
		FString MovementRotationMessage = FString::Printf(TEXT("Movement Rotation : %f"), MovementRotation.Yaw);
		FString OffsetMessage = FString::Printf(TEXT("MovementOffset Yaw : %f"), MovementOffsetYaw);
		if (GEngine)
		{
			//GEngine->AddOnScreenDebugMessage(1, 0.f, FColor::White, RotationMessage);
			GEngine->AddOnScreenDebugMessage(1, 0.f, FColor::White, OffsetMessage);
		}
		*/
	}
}

ShooterCharacter→GetBaseAimRotation()

Untitled

해당 함수를 통해서 우리는 캐릭터가 바라보는 전방에 대한 회전 값을 알 수 있다.

GIF 2023-03-02 오후 7-52-24.gif

Untitled

Velocity 를 사용한 Rotation 값 만들기

Untitled

Velocity 값을 통해서 X 축에 대한 회전값을 만들어 낸다.

이는 캐릭터가 이동하는 방향에 대한 회전 값을 알 수 있다. ( 이동하는 곳이 전방벡터가 된다고 생각하자 )

GIF 2023-03-02 오후 7-57-09.gif

위 두 값을 통해서…

내가 바라보고있는 방향의 Rotation 그리고 Velocity 값을 통해 만들어낸 Rotation 을 활용해서 우리는 두 Rotation 을 통해서 내가 전방을 바라보면서 움직일때 , 어느 방향으로 움직이는지 알아낼 수 있다.

그리고 그걸 만들어주는 함수가 바로 KismetMathLibaray 의 NormalizedDeltaRotator 이다.

Untitled

Untitled