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

View File

@@ -27,19 +27,36 @@ void AMyCharacter::StopRun(const FInputActionValue& Value)
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
AMyCharacter::AMyCharacter()
{
// 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;
GetCharacterMovement()->GetNavAgentPropertiesRef().bCanCrouch = true;
}
// Called when the game starts or when spawned
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
@@ -75,6 +92,10 @@ void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCompone
EnhancedInputComponent->BindAction(RunAction, ETriggerEvent::Started, this, &AMyCharacter::BeginRun);
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;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* RunAction;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input")
class UInputAction* CrouchAction;
UFUNCTION(BlueprintCallable, Category = "Move")
void Move(const FInputActionValue& Value);
@@ -25,6 +27,15 @@ public:
void BeginRun(const FInputActionValue& Value);
UFUNCTION(BlueprintCallable, Category = "Move")
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
AMyCharacter();
@@ -40,4 +51,5 @@ protected:
private:
bool bIsRunning = false;
bool bIsCrouching = false;
};