84 - Zoom While Aiming

This commit is contained in:
Kingsmedia 2022-05-05 21:56:31 +02:00
parent ad7af7454b
commit 38473505f5
6 changed files with 59 additions and 10 deletions

View File

@ -72,9 +72,10 @@ public:
void SetOverlappingWeapon(AWeapon* Weapon);
bool IsWeaponEquipped();
bool IsAiming();
FORCEINLINE float GetAO_Yaw() const { return AO_Yaw; };
FORCEINLINE float GetAO_Pitch() const { return AO_Pitch; };
FORCEINLINE float GetAO_Yaw() const { return AO_Yaw; }
FORCEINLINE float GetAO_Pitch() const { return AO_Pitch; }
AWeapon* GetEquippedWeapon();
FORCEINLINE ETurningInPlace GetTurningInPlace() const { return TurningInPlace; };
FORCEINLINE ETurningInPlace GetTurningInPlace() const { return TurningInPlace; }
FVector GetHitTarget() const;
FORCEINLINE UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};

View File

@ -7,6 +7,7 @@
#include "Blaster/HUD/BlasterHUD.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"
@ -35,20 +36,27 @@ void UCombatComponent::BeginPlay()
if (Character)
{
Character->GetCharacterMovement()->MaxWalkSpeed = BaseWalkSpeed;
if (Character->GetFollowCamera())
{
DefaultFOV = Character->GetFollowCamera()->FieldOfView;
CurrentFOV = DefaultFOV;
}
}
}
void UCombatComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
SetHUDCrosshairs(DeltaTime);
if (Character && Character->IsLocallyControlled())
{
FHitResult HitResult;
TraceUnderCrosshairs(HitResult);
HitTarget = HitResult.ImpactPoint;
SetHUDCrosshairs(DeltaTime);
InterpFOV(DeltaTime);
}
}
@ -105,6 +113,24 @@ void UCombatComponent::SetHUDCrosshairs(float DeltaTime)
}
}
void UCombatComponent::InterpFOV(float DeltaTime)
{
if (EquippedWeapon == nullptr) return;
if (bAiming)
{
CurrentFOV = FMath::FInterpTo(CurrentFOV, EquippedWeapon->GetZoomedFOV(), DeltaTime, EquippedWeapon->GetZoomInterpSpeed());
}
else
{
CurrentFOV = FMath::FInterpTo(CurrentFOV, DefaultFOV, DeltaTime, ZoomInterpSpeed);
}
if (Character && Character->GetFollowCamera())
{
Character->GetFollowCamera()->SetFieldOfView(CurrentFOV);
}
}
void UCombatComponent::SetAiming(bool bIsAiming)
{
bAiming = bIsAiming;

View File

@ -63,12 +63,24 @@ private:
bool bFireButtonPressed;
/*
* HUD and Crosshairs
*/
//HUD and Crosshairs
float CrosshairVelocityFactor;
float CrosshairInAirFactor;
// Aiming and FOV
// Field of view when not aiming; set to the camera's base FOV in BeginPlay
float DefaultFOV;
UPROPERTY(EditAnywhere, Category = Combat)
float ZoomedFOV = 30.f;
float CurrentFOV;
UPROPERTY(EditAnywhere, Category = Combat)
float ZoomInterpSpeed = 20.f;
void InterpFOV(float DeltaTime);
FVector HitTarget;
};

View File

@ -43,6 +43,13 @@ public:
UPROPERTY(EditAnywhere, Category= Crosshairs)
class UTexture2D* CrosshairsBottom;
// Zoom FOV while aiming
UPROPERTY(EditAnywhere)
float ZoomedFOV = 30.f;
UPROPERTY(EditAnywhere)
float ZoomInterpSpeed = 20.f;
protected:
virtual void BeginPlay() override;
@ -92,4 +99,7 @@ public:
void SetWeaponState(EWeaponState State);
FORCEINLINE USphereComponent* GetAreaSphere() const { return AreaSphere; }
FORCEINLINE USkeletalMeshComponent* GetWeaponMesh() const { return WeaponMesh; };
FORCEINLINE float GetZoomedFOV() const { return ZoomedFOV; };
FORCEINLINE float GetZoomInterpSpeed() const { return ZoomInterpSpeed; };
};