관리 메뉴

Mini

스테이트 머신 본문

UE5

스테이트 머신

Mini_96 2022. 8. 14. 13:52

목적 : 점프, 슬라이딩, 피격, 아이들 ... 등 수많은 애니메이션 정상출력

기존방법 : if else 사용 중첩, 반복 => (-)로직이 꼬임 ex) 슬라이딩중 피격되면???

개선 : 스테이트 머신 (하나의 상태를 두고 관리)

 

 

* 점프 구현

1. MyCharacter에서 jump (매핑될이름,조건(IE_Pressed == 눌린상태면) ,실행할 함수)

2. 엔진설정에서도 추가해주기

// Called to bind functionality to input
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction(TEXT("jump"), EInputEvent::IE_Pressed, this, &AMyCharacter::Jump);

	PlayerInputComponent->BindAxis(TEXT("UpDown"), this, &AMyCharacter::UpDown);
	PlayerInputComponent->BindAxis(TEXT("LeftRight"), this, &AMyCharacter::LeftRight);
	PlayerInputComponent->BindAxis(TEXT("Yaw"), this, &AMyCharacter::Yaw);

}

3. 스페이스 누르면 => Character의 IsFalling이 true가 될것.

4. 애니메이션에서 get으로 불러서 => 사용.

 

※ 점프함수는 부모인Character에 이미 구현되어 있다.

void ACharacter::Jump()
{
	bPressedJump = true;
	JumpKeyHoldTime = 0.0f;
}

 

* 애니메이션 클래스 => 변수저장

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyAnimInstance.h"
#include "GameFramework/Character.h"
#include "GameFramework/PawnMovementComponent.h"

void UMyAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
	Super::NativeUpdateAnimation(DeltaSeconds);

	auto Pawn = TryGetPawnOwner();
	if (IsValid(Pawn))
	{
		Speed = Pawn->GetVelocity().Size();

		auto Character = Cast<ACharacter>(Pawn);
		if (Character)
		{
			isFalling = Character->GetMovementComponent()->IsFalling();
		}
	}
}

1. 애니메이션 클래스에서 캐릭터의 정보를 불러온다.

2. 내 변수= 불러온 정보

3. 내 변수이용 => 애니메이션 분기

 

* 블루프린트 => 실제구현

상태들

 

Ground상태일때 애니메이션 분기
isFalling 이면 Ground상태에서 Jumping상태로 전환

 

 

* 애니메이션 개선 : Jump를 JumpStart, Jumping, JumpEnd로 나눔

=>자연스럽게

 

- 종료조건 : 애니메이션 남은시간 < 0.1(10%) -> JumpStart에서 Jumping으로 넘어가라

- JumpStart 애니메이션 -> 반복X -> Loop 체크 해제하라

 

- 종료조건 방법2 : Automatic 체크 => 애니메이션끝나면 자동으로 다음으로 넘어가라

'UE5' 카테고리의 다른 글

델레게이트  (0) 2022.08.14
애니메이션 몽타주  (0) 2022.08.14
애니메이션 기초  (0) 2022.08.14
블루프린트 클래스  (0) 2022.08.11
캐릭터 생성  (0) 2022.08.08