Unreal5 이동 애니메이션

Unreal5 이동 애니메이션.

안녕하세요.

오늘은 Idle상태와 Run상태의 애니메이션을 추가해보겠습니다.

이전에 작성한 글에서 이어서 작성하도록 하겠습니다.

[프로그래밍/언리얼] - Unreal5 Camera 와 Spring Arm C++

  • SlayerAnimInstance C++ 코드 생성.
  • Animation Blueprint.
  • 애니메이션 추가.
  • SlayerAnimInstance.h 작성.
  • SlayerAnimInstance.cpp 작성.
  • 마무리.

SlayerAnimInstance C++ 코드 생성.

AnimationInstance를 Next눌러서 생성해줍니다.

이름은 SlayerAnimationInstance로 하겠습니다.

Animation Blueprint.

Animation Blueprint를 하나 생성해서 이름을 ABP_Yin으로 설정해 주었습니다.

이후 AnimGraph에서 state Machine을 하나 생성해서 이름을 GroundLocomotion으로 설정했습니다.

여기서 Idle상태와 Run상태일 때 애니메이션을 추가해줄 겁니다.

GroundLocomotion으로 들어가서 IdleState와 RunState를 추가하고 둘이 이어주었습니다.

애니메이션 추가.

Idle상태에 들어가서 Idle에 맞는 애니메이션을 추가해줍니다.

animationd이 계속 실행되도록 Loop Animation도 켜줍니다.

Run에 들어가서 Run에 맞는 애니메이션도 추가해줍니다.

역시 Loop Animation을 켜줍니다.

 

SlayerAnimInstance.h 코드 작성.

public 으로 코드를 작성합니다.

NativeInitializeAnimation: 애니메이션 인스턴스가 초기화 될 때 필요한 작업을 수행.

NativeUpdateAnimation: 애니메이션 인스턴스가 각 프레임마다 업데이트 될 때 필요한 작업을 수행.

UCharacterMovementComponent: 캐릭터의 움직임과 관련된 정보와 기능을 담고 있는 컴포넌트.(걷는 속도, 낙하 속도, 지면과 충돌처리 등등. )

public:
	virtual void NativeInitializeAnimation() override;
	virtual void NativeUpdateAnimation(float DeltaTime) override;
	UPROPERTY(BlueprintReadOnly)
	class ASlayerCharacter* SlayerCharacter;
	UPROPERTY(BlueprintReadOnly, Category = Movement)
	class UCharacterMovementComponent* SlayerCharacterMovement;
	UPROPERTY(BlueprintReadOnly, Category = Movement)
	float GroundSpeed;

SlayerAnimInstance.cpp 코드 작성.

위에서 선언한 코드를 정의해줍니다.

// Fill out your copyright notice in the Description page of Project Settings.
#include "Character/SlayerAnimInstance.h"
#include "Character/SlayerCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/KismetMathLibrary.h"
void USlayerAnimInstance::NativeInitializeAnimation()
{
	Super::NativeInitializeAnimation();
	SlayerCharacter = Cast<ASlayerCharacter>(TryGetPawnOwner());
	if(SlayerCharacter){
		//#include "GameFramework/CharacterMovementComponent.h" 필요.
		SlayerCharacterMovement = SlayerCharacter->GetCharacterMovement();
	}
}

void USlayerAnimInstance::NativeUpdateAnimation(float DeltaTime)
{
	Super::NativeUpdateAnimation(DeltaTime);
	if (SlayerCharacterMovement) {
		//#include "Kismet/KismetMathLibrary.h"
		GroundSpeed = UKismetMathLibrary::VSizeXY(SlayerCharacterMovement->Velocity);
	}
}

https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/GameFramework/UCharacterMovementComponent/

 

UCharacterMovementComponent

CharacterMovementComponent handles movement logic for the associated Character owner.

docs.unrealengine.com

https://docs.unrealengine.com/4.26/en-US/API/Runtime/Engine/Kismet/UKismetMathLibrary/VSizeXY/

 

UKismetMathLibrary::VSizeXY

Returns the length of the vector's XY components.

docs.unrealengine.com

마무리.

C++코드를 다 작성했으니 블루프린트랑 연결해주겠습니다.

Class Settings를 누르고 Parent Class를 SlayerAnimInstance로 변경해주었습니다.

(아래 사진에서는 아직...)

이제 APB_Yin에서 Rule을 설정해 주겠습니다.

먼저 Idle to Run을 수정해 주겠습니다.

이 ABP_Yin은 SlayerAnimInstance를 상속받았으니 GroundSpeed를 가져올 수 있습니다.

GetGroundSpeed를 추가하고, Greater를 사용해서 0보다 크면 상태를 변경하도록 만들었습니다.

Run to Idle에서는 Get Ground를 하고, Equal를 사용해 0일때, 즉 멈춰있을 때 상태가 Idle로 변경하도록 설정하였습니다. 

해치웠나? 하고 실행해봤는데 애니메이션이 안되서 당황했습니다.

생각해보니 BP_SlayerCharacter에 ABP_Yin을 연결안했네요.

연결해주고 실행하니 잘 작동합니다.

물론 아직 점프 애니메이션도 못넣었고, 고개도 못돌리고 문제가 많아보이긴 하네요.

Designed by JB FACTORY