FPS 에서 캐릭터가 앞으로만 달릴 수 있도록 제한하는 로직을 구현
헤더 파일
FVector2D LastInputVector;
소스 파일 - Move 함수
void AMyCharacter::Move(const FInputActionValue& Value)
{
FVector2D MovementVector = Value.Get<FVector2D>();
LastInputVector = MovementVector;
if (Controller)
{
AddMovementInput(GetActorForwardVector(), MovementVector.X);
AddMovementInput(GetActorRightVector(), MovementVector.Y);
}
}
소스 파일 - StartSprint 함수
void AMyCharacter::StartSprint()
{
if (LastInputVector.X <= 0.f)
{
return;
}
...
}
소스 파일 - Tick 함수
void AMyCharacter::Tick(float DeltaTime)
{
...
if (CurrentState == ECharacterState::Sprinting)
{
if (LastInputVector.X <= 0.f)
{
StopSprint();
}
}
...
}
InputAction으로부터 받아오는 Value(Vector2D) 값을 저장하여 해당 값의 X 이 0 이하 일 경우 관련 로직들에서 Sprint 가 종료되도록 로직을 구현한다.
결과적으로 ECharacterState::Sprinting 상태에서 W 키에 입력이 멈출 경우 달리기를 할 수 없게 제한한다.