feat: add logic for jump, sprint and crouch

Added walk speed and sprint speed properties to character
Moved dodge properties to character
Implement logic for jump, sprint and crouch
Changed dodge key from Shift to Alt
This commit is contained in:
Kubson96 2025-02-22 23:31:23 +01:00
parent c26dcd6eae
commit 89da8c4463
5 changed files with 49 additions and 16 deletions

View File

@ -30,6 +30,9 @@ void AExoPlayerController::BeginPlay()
InteractionComponent = PlayerCharacter->FindComponentByClass<UInteractionComponent>();
ShootingComponent = PlayerCharacter->FindComponentByClass<UShootingComponent>();
// Ustawianie w komponencie poruszania prêdkoœci zapisanej w characterze
PlayerCharacter->GetCharacterMovement()->MaxWalkSpeed = PlayerCharacter->WalkSpeed;
}
void AExoPlayerController::PlayerTick(float DeltaTime)
@ -48,8 +51,10 @@ void AExoPlayerController::SetupInputComponent()
EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Triggered, this, &AExoPlayerController::Interact);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerJump);
EnhancedInputComponent->BindAction(DodgeAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerDodge);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerCrouch);
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSprint);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerStartCrouch);
EnhancedInputComponent->BindAction(CrouchAction, ETriggerEvent::Completed, this, &AExoPlayerController::PlayerStopCrouch);
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerStartSprint);
EnhancedInputComponent->BindAction(SprintAction, ETriggerEvent::Completed, this, &AExoPlayerController::PlayerStopSprint);
EnhancedInputComponent->BindAction(ShootAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerShoot);
EnhancedInputComponent->BindAction(AimAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerAim);
EnhancedInputComponent->BindAction(MeleAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerMeleAttack);
@ -90,7 +95,7 @@ void AExoPlayerController::Interact()
void AExoPlayerController::PlayerJump()
{
PlayerCharacter->Jump();
}
void AExoPlayerController::PlayerDodge()
@ -99,6 +104,8 @@ void AExoPlayerController::PlayerDodge()
if (PlayerCharacter->GetCharacterMovement()->IsFalling()) return;
if (PlayerCharacter->GetCharacterMovement()->IsCrouching()) return;
if (!CanDodge) return;
CanDodge = false;
@ -109,9 +116,9 @@ void AExoPlayerController::PlayerDodge()
if (DodgeDirection.IsNearlyZero())
DodgeDirection = PlayerCharacter->GetActorForwardVector();
PlayerCharacter->LaunchCharacter(DodgeDirection * DodgeForce, true, true);
PlayerCharacter->LaunchCharacter(DodgeDirection * PlayerCharacter->DodgeForce, true, true);
GetWorldTimerManager().SetTimer(DodgeCooldownTimer, this, &AExoPlayerController::ResetDodge, DodgeCooldown, false);
GetWorldTimerManager().SetTimer(DodgeCooldownTimer, this, &AExoPlayerController::ResetDodge, PlayerCharacter->DodgeCooldown, false);
}
void AExoPlayerController::ResetDodge()
@ -119,14 +126,28 @@ void AExoPlayerController::ResetDodge()
CanDodge = true;
}
void AExoPlayerController::PlayerCrouch()
void AExoPlayerController::PlayerStartCrouch()
{
PlayerCharacter->Crouch();
}
void AExoPlayerController::PlayerSprint()
void AExoPlayerController::PlayerStopCrouch()
{
PlayerCharacter->UnCrouch();
}
void AExoPlayerController::PlayerStartSprint()
{
UE_LOG(LogTemp, Display, TEXT("Start sprint"));
PlayerCharacter->GetCharacterMovement()->MaxWalkSpeed = PlayerCharacter->SprintSpeed;
}
void AExoPlayerController::PlayerStopSprint()
{
UE_LOG(LogTemp, Display, TEXT("Stop sprint"));
PlayerCharacter->GetCharacterMovement()->MaxWalkSpeed = PlayerCharacter->WalkSpeed;
}
void AExoPlayerController::PlayerShoot()

View File

@ -14,6 +14,18 @@ class EXO_API AExoPlayerCharacter : public AExoCharacterBase
public:
AExoPlayerCharacter();
UPROPERTY(EditAnywhere, Category = "Dodge Properties")
float DodgeForce = 5000.f;
UPROPERTY(EditAnywhere, Category = "Dodge Properties")
float DodgeCooldown = 2.f;
UPROPERTY(EditAnywhere, Category = "Movement Properties")
float WalkSpeed = 600.0f;
UPROPERTY(EditAnywhere, Category = "Movement Properties")
float SprintSpeed = 1500.0f;
protected:
virtual void BeginPlay() override;

View File

@ -48,10 +48,16 @@ protected:
void ResetDodge();
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerCrouch(); // LCtrl\C\X
void PlayerStartCrouch(); // LCtrl\C\X
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerSprint(); // LShift(Hold)
void PlayerStopCrouch(); // LCtrl\C\X
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerStartSprint(); // LShift(Hold)
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerStopSprint();
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerShoot(); // LPM
@ -96,12 +102,6 @@ protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Dodge Properties")
FTimerHandle DodgeCooldownTimer;
UPROPERTY(EditAnywhere, Category = "Dodge Properties")
float DodgeForce = 5000.f; // Move to player if future for better modification
UPROPERTY(EditAnywhere, Category = "Dodge Properties")
float DodgeCooldown = 2.f; // Move to player too.
UPROPERTY(EditAnywhere, Category = "Dodge Properties")
bool CanDodge = true;