Add functionality to change movement speed depending on status

This commit is contained in:
Louis Hobbs 2023-07-25 21:34:04 +01:00
parent 9e7417eb59
commit 3d4ef7e3c0
2 changed files with 39 additions and 2 deletions

View File

@ -242,12 +242,16 @@ void APlayerCharacter::QuitCallback(const FInputActionInstance& Instance)
void APlayerCharacter::SetSprintingCallback(const FInputActionInstance& Instance)
{
GetCharacterMovement()->MaxWalkSpeed = DefaultMovementSpeed * SprintSpeedMultiplier;
IsSpriting = true;
SetMovementSpeed();
}
void APlayerCharacter::SetWalkingCallback(const FInputActionInstance& Instance)
{
GetCharacterMovement()->MaxWalkSpeed = DefaultMovementSpeed;
IsSpriting = false;
SetMovementSpeed();
}
void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
@ -375,6 +379,22 @@ void APlayerCharacter::OnDeath()
ClearAllTimers();
}
void APlayerCharacter::SetMovementSpeed()
{
if (IsADS)
{
GetCharacterMovement()->MaxWalkSpeed = DefaultMovementSpeed * ADSSpeedMultiplier;
}
else if (IsSpriting)
{
GetCharacterMovement()->MaxWalkSpeed = DefaultMovementSpeed * SprintSpeedMultiplier;
}
else
{
GetCharacterMovement()->MaxWalkSpeed = DefaultMovementSpeed;
}
}
void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Instance)
{
float value = Instance.GetValue().Get<float>();
@ -391,6 +411,10 @@ void APlayerCharacter::WeaponSwitchingCallback(const FInputActionInstance& Insta
void APlayerCharacter::BeginAimDownSightsCallback(const FInputActionInstance& Instance)
{
IsADS = true;
SetMovementSpeed();
FLatentActionInfo LatentActionInfo;
LatentActionInfo.CallbackTarget = this;
CameraComponent->AttachToComponent(CameraADSSpringArmComponent, FAttachmentTransformRules::KeepWorldTransform,
@ -403,6 +427,10 @@ void APlayerCharacter::BeginAimDownSightsCallback(const FInputActionInstance& In
void APlayerCharacter::EndAimDownSightsCallback(const FInputActionInstance& Instance)
{
IsADS = false;
SetMovementSpeed();
FLatentActionInfo LatentActionInfo;
LatentActionInfo.CallbackTarget = this;
CameraComponent->AttachToComponent(CameraSpringArmComponent, FAttachmentTransformRules::KeepWorldTransform,

View File

@ -65,6 +65,9 @@ protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float SprintSpeedMultiplier = 2.0f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float ADSSpeedMultiplier = 0.5f;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
float CameraBlendTime = 0.1f;
@ -91,6 +94,10 @@ private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionStimuliSourceComponent* PerceptionSource;
bool IsSpriting = false;
bool IsADS = false;
public:
// Sets default values for this character's properties
APlayerCharacter();
@ -159,4 +166,6 @@ protected:
virtual void OnDamaged() override;
virtual void OnDeath() override;
void SetMovementSpeed();
};