Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "LD_Character.h"
- #include "GameFramework/CharacterMovementComponent.h"
- // Sets default values
- ALD_Character::ALD_Character(): SelfReference(this) // Initialize SelfReference in the constructor initializer list
- {
- PrimaryActorTick.bCanEverTick = true;
- // Initialize other variables
- bIsSprinting = false;
- CurrentSprintDuration = 0.0f;
- SprintStartTime = 0.0f;
- MaxSprintDuration = 10.0f;
- WalkSpeed = 600.0f;
- SprintSpeed = 10000.0f;
- }
- // Called when the game starts or when spawned
- void ALD_Character::BeginPlay()
- {
- Super::BeginPlay();
- }
- // Called every frame
- void ALD_Character::Tick(float DeltaTime)
- {
- Super::Tick(DeltaTime);
- }
- // Called to bind functionality to input
- void ALD_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- }
- void ALD_Character::UpdateSprintSpeed()
- {
- if (!bIsSprinting)
- {
- return;
- }
- // Calculate how long we have been sprinting
- CurrentSprintDuration = GetWorld()->GetTimeSeconds() - SprintStartTime;
- // Ensure we do not exceed the max sprint duration
- CurrentSprintDuration = FMath::Clamp(CurrentSprintDuration, 0.0f, MaxSprintDuration);
- // Interpolate between walk speed and sprint speed based on the current sprint duration
- float SprintAlpha = CurrentSprintDuration / MaxSprintDuration;
- float NewSpeed = FMath::Lerp(WalkSpeed, SprintSpeed, SprintAlpha);
- // Update the character's movement component with the new speed
- GetCharacterMovement()->MaxWalkSpeed = NewSpeed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement