feat: 增加蹲下功能

This commit is contained in:
meishibiezb
2026-03-15 01:39:06 +08:00
parent f5767bb654
commit 517e3377b2
5 changed files with 41 additions and 5 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -27,12 +27,29 @@ void AMyCharacter::StopRun(const FInputActionValue& Value)
GetCharacterMovement()->MaxWalkSpeed = 600.0f; GetCharacterMovement()->MaxWalkSpeed = 600.0f;
} }
void AMyCharacter::BeginCrouch(const FInputActionValue& Value)
{
bIsCrouching = true;
GetCharacterMovement()->bWantsToCrouch = true;
GetCharacterMovement()->Crouch();
UE_LOG(LogTemp, Warning, TEXT("Crouching"));
}
void AMyCharacter::StopCrouch(const FInputActionValue& Value)
{
bIsCrouching = false;
GetCharacterMovement()->bWantsToCrouch = false;
GetCharacterMovement()->UnCrouch();
UE_LOG(LogTemp, Warning, TEXT("Stop Crouch"));
}
// Sets default values // Sets default values
AMyCharacter::AMyCharacter() AMyCharacter::AMyCharacter()
{ {
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
} }
// Called when the game starts or when spawned // Called when the game starts or when spawned
@@ -75,6 +92,10 @@ void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCompone
EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Started, this, &AMyCharacter::BeginRun); EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Started, this, &AMyCharacter::BeginRun);
EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Completed, this, &AMyCharacter::StopRun); EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Completed, this, &AMyCharacter::StopRun);
} }
if (CrouchAction) {
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AMyCharacter::BeginCrouch);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Completed, this, &AMyCharacter::StopCrouch);
}
} }
} }

View File

@@ -18,6 +18,8 @@ public:
class UInputAction* MoveAction; class UInputAction* MoveAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input") UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* RunAction; class UInputAction* RunAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* CrouchAction;
UFUNCTION(BlueprintCallable, Category = "Move") UFUNCTION(BlueprintCallable, Category = "Move")
void Move(const FInputActionValue& Value); void Move(const FInputActionValue& Value);
@@ -25,6 +27,15 @@ public:
void BeginRun(const FInputActionValue& Value); void BeginRun(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Move") UFUNCTION(BlueprintCallable, Category = "Move")
void StopRun(const FInputActionValue& Value); void StopRun(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Move")
void BeginCrouch(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Move")
void StopCrouch(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "State")
bool IsRunning() const { return bIsRunning; }
UFUNCTION(BlueprintCallable, Category = "State")
bool IsCrouching() const { return bIsCrouching; }
// Sets default values for this character's properties // Sets default values for this character's properties
AMyCharacter(); AMyCharacter();
@@ -40,4 +51,5 @@ protected:
private: private:
bool bIsRunning = false; bool bIsRunning = false;
bool bIsCrouching = false;
}; };