Advertisement
Shiny_

Untitled

Dec 3rd, 2023 (edited)
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "LD_Character.h"
  4. #include "GameFramework/CharacterMovementComponent.h"
  5.  
  6. // Sets default values
  7. ALD_Character::ALD_Character(): SelfReference(this) // Initialize SelfReference in the constructor initializer list
  8. {
  9.     PrimaryActorTick.bCanEverTick = true;
  10.  
  11.     // Initialize other variables
  12.     bIsSprinting = false;
  13.     CurrentSprintDuration = 0.0f;
  14.     SprintStartTime = 0.0f;
  15.     MaxSprintDuration = 10.0f;
  16.     WalkSpeed = 600.0f;
  17.     SprintSpeed = 10000.0f;
  18. }
  19.  
  20. // Called when the game starts or when spawned
  21. void ALD_Character::BeginPlay()
  22. {
  23.     Super::BeginPlay();
  24.    
  25. }
  26.  
  27. // Called every frame
  28. void ALD_Character::Tick(float DeltaTime)
  29. {
  30.     Super::Tick(DeltaTime);
  31.  
  32. }
  33.  
  34. // Called to bind functionality to input
  35. void ALD_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  36. {
  37.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  38.  
  39. }
  40.  
  41. void ALD_Character::UpdateSprintSpeed()
  42. {
  43.     if (!bIsSprinting)
  44.     {
  45.         return;
  46.     }
  47.  
  48.     // Calculate how long we have been sprinting
  49.     CurrentSprintDuration = GetWorld()->GetTimeSeconds() - SprintStartTime;
  50.  
  51.     // Ensure we do not exceed the max sprint duration
  52.     CurrentSprintDuration = FMath::Clamp(CurrentSprintDuration, 0.0f, MaxSprintDuration);
  53.  
  54.     // Interpolate between walk speed and sprint speed based on the current sprint duration
  55.     float SprintAlpha = CurrentSprintDuration / MaxSprintDuration;
  56.     float NewSpeed = FMath::Lerp(WalkSpeed, SprintSpeed, SprintAlpha);
  57.  
  58.     // Update the character's movement component with the new speed
  59.     GetCharacterMovement()->MaxWalkSpeed = NewSpeed;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement