feat: add aiming

Added aiming feature, possible when holding gun. Aiming FOV depends on gun
This commit is contained in:
Kubson96 2025-04-04 16:33:54 +02:00
parent fa9cf7ef3b
commit 0b52fe2b4f
6 changed files with 57 additions and 10 deletions

View File

@ -20,8 +20,11 @@ void UShootingComponent::BeginPlay()
Super::BeginPlay();
PlayerCharacter = Cast<AExoPlayerCharacter>(GetOwner());
}
CameraManager = GetWorld()->GetFirstPlayerController()->PlayerCameraManager;
DefaultFOV = CameraManager->GetFOVAngle();
TargetFOV = DefaultFOV;
}
void UShootingComponent::Shoot()
{
@ -88,6 +91,7 @@ void UShootingComponent::Reload()
if (!IsValid(CurrentGun) || bIsReloading) return;
bIsReloading = true;
StopAiming();
CurrentGun->CurrentAmmo = CurrentGun->MaxAmmo;
PlayerCharacter->GetWorldTimerManager().SetTimer(ReloadTimer, this, &UShootingComponent::ReloadCompleted, CurrentGun->ReloadTime, false);
@ -120,6 +124,8 @@ void UShootingComponent::PickUpGun(AGunBase* gunItem)
void UShootingComponent::DropGun()
{
if (!IsValid(CurrentGun)) return;
StopAiming();
FVector ForwardVector = PlayerCharacter->GetActorForwardVector();
FVector PlayerPos = PlayerCharacter->GetActorLocation();
@ -168,6 +174,7 @@ void UShootingComponent::SwitchGun()
SecondaryGun = temp;
bIsFirstGunSelected = !bIsFirstGunSelected;
StopAiming();
// Debug info
if (bIsFirstGunSelected)
@ -180,11 +187,25 @@ void UShootingComponent::SwitchGun()
}
}
void UShootingComponent::StartAiming()
{
if (IsValid(CurrentGun))
{
TargetFOV = CurrentGun->AimingFOV;
}
}
void UShootingComponent::StopAiming()
{
TargetFOV = DefaultFOV;
}
// Called every frame
void UShootingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
float CurrentFOV = CameraManager->GetFOVAngle();
float NewFOV = FMath::FInterpTo(CurrentFOV, TargetFOV, DeltaTime, AimingSpeed);
CameraManager->SetFOV(NewFOV);
}

View File

@ -57,7 +57,8 @@ void AExoPlayerController::SetupInputComponent()
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(AimAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerStartAim);
EnhancedInputComponent->BindAction(AimAction, ETriggerEvent::Completed, this, &AExoPlayerController::PlayerStopAim);
EnhancedInputComponent->BindAction(MeleAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerMeleAttack);
EnhancedInputComponent->BindAction(ChangeWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerChangeWeapon);
EnhancedInputComponent->BindAction(SelectFirstWeaponAction, ETriggerEvent::Started, this, &AExoPlayerController::PlayerSelectFirstWeapon);
@ -159,9 +160,14 @@ void AExoPlayerController::PlayerShoot()
ShootingComponent->Shoot();
}
void AExoPlayerController::PlayerAim()
void AExoPlayerController::PlayerStartAim()
{
ShootingComponent->StartAiming();
}
void AExoPlayerController::PlayerStopAim()
{
ShootingComponent->StopAiming();
}
void AExoPlayerController::PlayerMeleAttack()

View File

@ -25,6 +25,9 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Character")
TObjectPtr<AExoPlayerCharacter> PlayerCharacter;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
TObjectPtr<APlayerCameraManager> CameraManager;
protected:
// Called when the game starts
virtual void BeginPlay() override;
@ -54,6 +57,15 @@ public:
UFUNCTION(Category = "Shooting")
void SelectGun(bool bSelectFirstSlot);
UFUNCTION(Category = "Shooting")
void StartAiming();
UFUNCTION(Category = "Shooting")
void StopAiming();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shooting")
float AimingSpeed = 30.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Shooting")
float DropGunRange = 20.0f;
@ -74,6 +86,9 @@ private:
bool bCanShoot = true;
bool bIsReloading = false;
float DefaultFOV;
float TargetFOV;
bool bIsFirstGunSelected = true;
float MeleDamageValue = 50.f;

View File

@ -31,6 +31,9 @@ public:
UPROPERTY(EditAnywhere, Category = "Shooting")
float ReloadTime = 3.0f;
UPROPERTY(EditAnywhere, Category = "Shooting")
float AimingFOV = 55.0f;
UPROPERTY(EditAnywhere, Category = "Ammo")
int32 CurrentAmmo = 10;

View File

@ -63,7 +63,10 @@ protected:
void PlayerShoot(); // LPM
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerAim(); // PPM - nieprzypisane
void PlayerStartAim(); // PPM (pressed)
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerStopAim(); // PPM (released)
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerMeleAttack(); // V - nieprzypisane
@ -82,9 +85,8 @@ protected:
UFUNCTION(BlueprintCallable, Category = "Input")
void PlayerReload(); // R
protected:
UPROPERTY(EditAnywhere, Category = "Input")
TObjectPtr<UInputMappingContext> InputContext;