캐릭터 CustomCrouch

GwakItect·2025년 7월 31일

Shooter Game Project

목록 보기
5/9
post-thumbnail

커스텀 앉기 로직


  • 문제점: 기존 Unreal 내장 Crouch UnCrouch 는 순간적으로 앉기/서기를 동작 없이 전환하여 부자연스러움
    따라서 Timeline 과 선형 보간을 활용하여 앉기/서기 전환을 자연스럽게 구현




Float Curve 파일


간단하게 시간은 0 - 0.3, 값은 0 - 1 로 설정




소스 코드


헤더 파일

UFUNCTION() // <- 리플렉션 절대 넣을것
void UpdateCrouch(float Value);
    
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Movement")
TObjectPtr<UCurveFloat> CrouchCurve;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Movement")
TObjectPtr<UTimelineComponent> CrouchTimeline;

소스 파일 - 생성자

CrouchTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("CrouchTimeline"));

소스 파일 - BeginPlay 함수

if (CrouchCurve && CrouchTimeline)
{
	FOnTimelineFloat CrouchInterpFunction;
	CrouchInterpFunction.BindUFunction(this, FName("UpdateCrouch"));
    CrouchTimeline->AddInterpFloat(CrouchCurve, CrouchInterpFunction);
}

소스 파일 - 추가 함수

void AMyCharacter::UpdateCrouch(float Value)
{
	const float NewHalfHeight = FMath::Lerp(StandingCapsuleHalfHeight, CrouchingCapsuleHalfHeight, Value);
	GetCapsuleComponent()->SetCapsuleHalfHeight(NewHalfHeight);
}

소스 파일 - 기존 Crouch 함수

void AMyCharacter::StartCrouch()
{
	StopSprint();
	
	if (CrouchTimeline)
	{
		CrouchTimeline->PlayFromStart();
	}
	bWantsToSprint = false;
	bIsCrouching = true;
	ApplyMovementSpeedByState();
}

void AMyCharacter::EndCrouch()
{
	if (CrouchTimeline)
	{
		CrouchTimeline->Reverse();
	}
	bIsCrouching = false;
	ApplyMovementSpeedByState();
}

내장 함수 Crouch UnCrouch 를 제거하고 타임라인 바인딩 함수 호출




결과 및 정리


내장 함수를 사용했을 때와 다르게 부드럽게 앉는 것이 표현되는 것을 확인


커스텀 앉기 로직 (Timeline 사용)

  1. BeginPlay: CrouchCurve 에셋과 UpdateCrouch 함수를 CrouchTimeline에 바인딩
  2. StartCrouch: CrouchTimeline->PlayFromStart()를 호출하여 타임라인을 재생
  3. EndCrouch: CrouchTimeline->Reverse()를 호출하여 타임라인을 역재생
  4. UpdateCrouch(float Alpha): 타임라인이 재생되는 동안 커브 에셋으로부터 0~1 사이의 Value 값을 받아, FMath::Lerp를 통해 서 있을 때와 앉았을 때의 캡슐 높이를 부드럽게 보간

0개의 댓글