언리얼 개인 프로젝트/언리얼엔진C++ 독학(이득우)

7.애니메이션 시스템의 설계_1)달리기와 멈춤 애니메이션

현구구 2022. 3. 5. 22:52

새로운 c++클래스에서 AnimInstance 를 선택하고 이름을 ABAnimInstance 라고 지어준다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<ABAnimInstance.h>
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "ArenaBattle.h" // = EngineMinimal.h
#include "Animation/AnimInstance.h"
#include "ABAnimInstance.generated.h"
 
/**
 * 
 */
UCLASS()
class ARENABATTLE_API UABAnimInstance : public UAnimInstance
{
    GENERATED_BODY()
    
public:
    UABAnimInstance();
 
private:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Pawn, Meta = (AllowPrivateAccess = true))
    float CurrentPawnSpeed;
};
 
cs

h파일에서 블루프린트에서 편집할 수 있게 UPROPERTY를 선언하고 CurrentPawnSpeed 를 선언한다

 

1
2
3
4
5
6
7
8
9
10
<ABAnimInstance.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "ABAnimInstance.h"
 
UABAnimInstance::UABAnimInstance()
{
    CurrentPawnSpeed = 0.0f;
}
cs

 

이후 cpp 파일에서 CurrentPawnSpeed 변수를 초기화 해준다

그리고 WarriorAnimBluprint에 가서 클래스세팅을 누르고 부모 클래스를 방금 만든 ABAnim Instance로 바꾸어준다

이후 h파일에서 선언한 CurrentPawnSpeed 를 블루프린트에 드래그하고 Get CurrentPawnSpeed 를 눌러 노드를 생성한다

노드에서 선을 연결해 float>float 노드를 생성하고 연결한다

그리고 bool로 포즈 블렌딩 노드를 넣고 True일 땐 움직이는 애니메이션을, False일 땐 가만히 있는 애니메이션을 연결해준다.

이제 CurrentPawnSpeed 가 0.0보다 클 때에는 True 에 연결되어 있는 움직이는 애니메이션이 실행된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<ABAnimInstance.h>
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "ArenaBattle.h" // = EngineMinimal.h
#include "Animation/AnimInstance.h"
#include "ABAnimInstance.generated.h"
 
/**
 * 
 */
UCLASS()
class ARENABATTLE_API UABAnimInstance : public UAnimInstance
{
    GENERATED_BODY()
    
public:
    UABAnimInstance();
    virtual void NativeUpdateAnimation(float DeltaSeconds) override;
 
...
};
 
cs

 

AnimInstance 의 Tick함수와 같은 NativeUpDateAnimation 함수를 선언한다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<ABAnimInstance.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "ABAnimInstance.h"
 
UABAnimInstance::UABAnimInstance()
{
    CurrentPawnSpeed = 0.0f;
}
 
void UABAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
    Super::NativeUpdateAnimation(DeltaSeconds);
 
    auto Pawn = TryGetPawnOwner();
    if (::IsValid(Pawn))
    {
        CurrentPawnSpeed = Pawn->GetVelocity().Size();
    } //CurrentPawnSpeed 에 현재 폰의 속력을 대입한다.
}
 
cs

 

그리고 cpp파일에서 매 DeltaSeconds 마다 폰의 현재 속도를 받아와 CurrentPawnSpeed 에 대입한다.

컴파일하고 플레이 해보면 캐릭터가 가만히 있을 때는 멈추고 움직일 때는 움직이는 애니메이션이 작동되는 것을 확인 할 수 있다.