Added weapon recoil for all future guns

This commit is contained in:
MARCEL HARA 2023-10-05 12:24:18 +01:00
parent e9cacdb064
commit fed628ed77
7 changed files with 128 additions and 9 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:fc4caf67df37627a19c4b9f2c464d84cb293574db45f018458c0d59e0a15ba1a oid sha256:cff6a88ce5641de43b7168dbe156dc6c743108fd4fe22dd89647224cdc5249fc
size 37750 size 37804

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:77b4872fd2fde68620f3fb539222718cbe2c66160a956c6373d3650c58a9bf88 oid sha256:c12dd919c67095a4cdc8439b9a0e8c25d5d65566abbd657d7b43bcdf6dac24b8
size 29117 size 29704

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5f4479a0f7cd596b9f9d263ba4898b07487d8e065ec43bd14dd4ea87d239695d
size 2006

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:f9dd9531bbf28069122709420605ec7c6478ddea44b642b28f016b2349a05597 oid sha256:1beca35b5b67f492442adf27a1933f51133cce10ed01c3ae69b21a371efe6c4e
size 5061 size 5061

View File

@ -45,8 +45,80 @@ void ABaseWeaponClass::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
if (player)
{
ApplyRecoil(DeltaTime);
}
} }
// Recoil Handling //
void ABaseWeaponClass::GenerateRecoilVector()
{
float angle = FMath::RandRange(recoilMaxAngleLeft, -recoilMaxAngleRight); //randomg recoil vector angle between left and right
float recMag = recoilMagnitude * 252.f; //converting degrees to controller units
float tempMag = -FMath::RandRange(recMag * recoilMinMultiplier, recMag); // recoil magnitude
recoilResultYaw = FMath::Sin(FMath::DegreesToRadians(angle));
recoilResultPitch = FMath::Cos(FMath::DegreesToRadians(angle));
//scaling direction to magnitude
recoilResultPitch *= -tempMag;
recoilResultYaw *= tempMag;
//reset recoil to 0 of curve
recoilTime = 0;
}
void ABaseWeaponClass::ApplyRecoil(float DeltaTime)
{
if (recoilTime < 0.3)
{
float amplitude = RecoilCurve->GetFloatValue(recoilTime); //get current value of curve in time
recoilTime += DeltaTime;
player->AddControllerPitchInput(GetRecoilPitch(amplitude, recoilTime));
player->AddControllerYawInput(GetRecoilYaw(amplitude, recoilTime));
UpdateSamples(amplitude, recoilTime);
}
}
void ABaseWeaponClass::nullSamples()
{
recoilCurvet = 0;
recoilCurvez1 = 0;
}
float ABaseWeaponClass::GetRecoilPitch(float Amp, float Time)
{
//Using the trapez method and we are getting the surface under the curve, each trapezoid consist of square and right triangle
float lower = recoilCurvez1 < Amp ? recoilCurvez1 : Amp; //get which point is common for both triangle and square of trapezoid
//lower point
float mult = (Time - recoilCurvet) * lower; //getting surface of square
mult += (Time - recoilCurvet) * (Amp - recoilCurvez1) / 2.0f; //getting and adding surface of triangle
return (recoilResultPitch * mult) / playerControllerRef->InputPitchScale_DEPRECATED; //calculating and return recoil force for current frame
}
//Same as getrecoilpitch
float ABaseWeaponClass::GetRecoilYaw(float Amp, float Time)
{
float lower = recoilCurvez1 < Amp ? recoilCurvez1 : Amp;
float mult = (Time - recoilCurvet) * lower;
mult += (Time - recoilCurvet) * (Amp - recoilCurvez1) / 2.0f;
return (recoilResultYaw * mult) / playerControllerRef->InputYawScale_DEPRECATED;
}
void ABaseWeaponClass::UpdateSamples(float Amp, float Time)
{
recoilCurvez1 = Amp;
recoilCurvet = Time;
}
// Fire handling //
void ABaseWeaponClass::ClickDetectionTimer() void ABaseWeaponClass::ClickDetectionTimer()
{ {
GetWorldTimerManager().SetTimer(timerHandle, this, &ABaseWeaponClass::Fire, 1 / FireRate, false); GetWorldTimerManager().SetTimer(timerHandle, this, &ABaseWeaponClass::Fire, 1 / FireRate, false);
@ -57,7 +129,6 @@ void ABaseWeaponClass::CancelFire()
GetWorldTimerManager().ClearTimer(timerHandle); GetWorldTimerManager().ClearTimer(timerHandle);
} }
void ABaseWeaponClass::Fire() void ABaseWeaponClass::Fire()
{ {
//do damage fallof based off distance //do damage fallof based off distance
@ -77,6 +148,7 @@ void ABaseWeaponClass::Fire()
//Debug line to see where the trace hit //Debug line to see where the trace hit
DrawDebugLine(this->GetWorld(), traceStart, traceEnd, FColor::Red, false, 5.0f, 0U, 2.5f); DrawDebugLine(this->GetWorld(), traceStart, traceEnd, FColor::Red, false, 5.0f, 0U, 2.5f);
playerControllerRef->PlayerCameraManager->StartCameraShake(CameraShakeClass, 1); playerControllerRef->PlayerCameraManager->StartCameraShake(CameraShakeClass, 1);
GenerateRecoilVector();
ClickDetectionTimer(); ClickDetectionTimer();
if (outHit.bBlockingHit) if (outHit.bBlockingHit)
{ {

View File

@ -24,6 +24,23 @@ protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
void GenerateRecoilVector();
void ApplyRecoil(float DeltaTime);
void nullSamples();
//recoil previous curve
float recoilCurvez1 = 0;
//recoil previous curve time
float recoilCurvet = 0;
float recoilTime = 0;
float recoilResultPitch = 0;
float recoilResultYaw = 0;
public: public:
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
@ -64,6 +81,33 @@ public:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
TSubclassOf<UCameraShakeBase> CameraShakeClass; TSubclassOf<UCameraShakeBase> CameraShakeClass;
/* How far the gun goes up*/
UPROPERTY(EditAnywhere, Category = "Recoil")
float recoilMagnitude;
UPROPERTY(EditAnywhere, Category = "Recoil")
float recoilMaxAngleLeft;
UPROPERTY(EditAnywhere, Category = "Recoil")
float recoilMaxAngleRight;
UPROPERTY(EditAnywhere, Category = "Recoil")
float recoilMinMultiplier;
UPROPERTY(EditAnywhere, Category = "Recoil")
class UCurveFloat* RecoilCurve;
UFUNCTION(BlueprintCallable, Category = "Recoil")
float GetRecoilPitch(float Amp, float Time);
UFUNCTION(BlueprintCallable, Category = "Recoil")
float GetRecoilYaw(float Amp, float Time);
UFUNCTION(BlueprintCallable, Category = "Recoil")
void UpdateSamples(float Amp, float Time);
private: private:
UArrowComponent* GunStartArrow; UArrowComponent* GunStartArrow;