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

7.애니메이션 시스템의 설계_2)스테이트 머신,점프 구현1

현구구 2022. 3. 5. 23:34

기존 Result의 연결을 끊고 스테이트 머신 새로추가를 해서 Result와 연결해준다

스테이트를 추가하고 이름은 Ground로 해준다

그 다음 Ground로 들어가 아까 한 작업들을 복붙해준다

--------점프 구현-------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<ABAnimInstance.cpp>
#include "ABCharacter.h"
 
// Sets default values
AABCharacter::AABCharacter()
{
     ...
    GetCharacterMovement()->JumpZVelocity = 800.0f;//점프 높이를 800으로 설정
}
 
...
// Called to bind functionality to input
void AABCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    ...
 
    PlayerInputComponent->BindAction(TEXT("Jump"), EInputEvent::IE_Pressed, this&ACharacter::Jump);
}
 
...
cs

cpp파일에서 프로젝스 세팅-입력에 있는 Jump를 묶기위해 SetupPlayerInputComponent 에 Jump를 묶어주고

생성자에서 점프 높이를 800으로 초기화해준다.

캐릭터의 MovementComponent에서 제공하는 4가지 함수들이 있다

IsFalling() : 현재 공중에 떠있는지 알려준다

IsSwimming() : 현재 수영중인지 알려준다

IsCrouching() : 현재 앉아있는지 알려준다

IsMoveOnGround() : 현재 땅에서 이동중인지 알려준다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<ABAnimInstance.h>
// Fill out your copyright notice in the Description page of Project Settings.
 
...
 
/**
 * 
 */
UCLASS()
class ARENABATTLE_API UABAnimInstance : public UAnimInstance
{
    ...
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Pawn, Meta = ((AllowPrivateAccess = true)))
    bool IsInAir;
};
cs

우선 h파일에서 IsFalling 통해 캐릭터가 공중에 있는지(True), 공중에 있지 않은지(False)를 받아줄 bool형 IsInAir를 선언해준다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<ABAnimInstance.cpp>
// Fill out your copyright notice in the Description page of Project Settings.
 
 
#include "ABAnimInstance.h"
 
UABAnimInstance::UABAnimInstance()
{
    CurrentPawnSpeed = 0.0f;
    IsInAir = false;//디폴트를 false 로 해서 평소에는 땅위에있음
}
 
void UABAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
    Super::NativeUpdateAnimation(DeltaSeconds);
 
    auto Pawn = TryGetPawnOwner();
    if (::IsValid(Pawn))
    {
        CurrentPawnSpeed = Pawn->GetVelocity().Size();
        //CurrentPawnSpeed 에 현재 폰의 속력을 대입한다.
        auto Character = Cast<ACharacter>(Pawn);
        if (Character)
        {
            IsInAir = Character->GetMovementComponent()->IsFalling();
            //공중에 있을 때 True 값을 IsInAir에 전달
        }
    } 
}
 
cs

이후 cpp파일에서 IsInAir를 false로 초기화해주고 캐릭터가 공중에 있을 때 IsInAir값이 True로 바뀔 수 있도록 만들어준다

이제 블루프린트로 가서 새로운 스테이트를 만들고 이름은 Jump라고 한다 이제 IsInAir 가 True일 경우

Ground 에서 Jump 로 이동할 것이고 IsInAir가 False일 경우 다시 Jump에서 Ground로 애니메이션이 이동하게 노드를 짜준다.

Ground->Jump
Jump->Ground

컴파일하고 게임을 실행해보면 이제 점프를 할 동안 기본자세로 돌아가는 것을 볼 수 있다.