174 - Replicating Shotgun Shatter

This commit is contained in:
Kingsmedia 2022-05-26 14:29:39 +02:00
parent e107458690
commit 82e10b18a6
5 changed files with 54 additions and 19 deletions

View File

@ -5,6 +5,7 @@
#include "Blaster/Character/BlasterCharacter.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/Weapon/Shotgun.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Engine/SkeletalMeshSocket.h"
@ -124,8 +125,12 @@ void UCombatComponent::Fire()
void UCombatComponent::FireProjectileWeapon()
{
LocalFire(HitTarget);
ServerFire(HitTarget);
if (PrimaryWeapon)
{
HitTarget = PrimaryWeapon->bUseScatter ? PrimaryWeapon->TraceEndWithScatter(HitTarget) : HitTarget;
LocalFire(HitTarget);
ServerFire(HitTarget);
}
}
void UCombatComponent::FireHitScanWeapon()
@ -140,6 +145,11 @@ void UCombatComponent::FireHitScanWeapon()
void UCombatComponent::FireShotgun()
{
if (AShotgun* Shotgun = Cast<AShotgun>(PrimaryWeapon))
{
TArray<FVector> HitTargets;
Shotgun->ShotgunTraceEndWithScatter(HitTarget, HitTargets);
}
}

View File

@ -6,6 +6,7 @@
#include "Blaster/Character/BlasterCharacter.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
void AShotgun::Fire(const FVector& HitTarget)
{
@ -75,3 +76,26 @@ void AShotgun::Fire(const FVector& HitTarget)
}
}
}
void AShotgun::ShotgunTraceEndWithScatter(const FVector& HitTarget, TArray<FVector>& HitTargets)
{
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
if (MuzzleFlashSocket == nullptr) return;
const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
const FVector TraceStart = SocketTransform.GetLocation();
const FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal();
const FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere;
for (uint32 i = 0; i < NumberOfPellets; i++)
{
const FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius);
const FVector EndLoc = SphereCenter + RandVec;
FVector ToEndLoc = EndLoc - TraceStart;
ToEndLoc = FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size())
HitTargets.Add(ToEndLoc);
}
}

View File

@ -17,6 +17,7 @@ class BLASTER_API AShotgun : public AHitScanWeapon
public:
virtual void Fire(const FVector& HitTarget) override;
void ShotgunTraceEndWithScatter(const FVector& HitTarget, TArray<FVector>& HitTargets);
private:

View File

@ -298,15 +298,15 @@ FVector AWeapon::TraceEndWithScatter(const FVector& HitTarget)
{
const USkeletalMeshSocket* MuzzleFlashSocket = GetWeaponMesh()->GetSocketByName("MuzzleFlash");
if (MuzzleFlashSocket == nullptr) return FVector();
FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
FVector TraceStart = SocketTransform.GetLocation();
FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal();
FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere;
FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius);
FVector EndLoc = SphereCenter + RandVec;
FVector ToEndLoc = EndLoc - TraceStart;
const FTransform SocketTransform = MuzzleFlashSocket->GetSocketTransform(GetWeaponMesh());
const FVector TraceStart = SocketTransform.GetLocation();
const FVector ToTargetNormalized = (HitTarget - TraceStart).GetSafeNormal();
const FVector SphereCenter = TraceStart + ToTargetNormalized * DistanceToSphere;
const FVector RandVec = UKismetMathLibrary::RandomUnitVector() * FMath::FRandRange(0.f, SphereRadius);
const FVector EndLoc = SphereCenter + RandVec;
const FVector ToEndLoc = EndLoc - TraceStart;
/*
DrawDebugSphere(GetWorld(), SphereCenter, SphereRadius, 12, FColor::Red, true);

View File

@ -116,6 +116,14 @@ protected:
int32 OtherBodyIndex
);
// Trace end with scatter
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float DistanceToSphere = 800.f;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float SphereRadius = 75.f;
private:
UPROPERTY(VisibleAnywhere, Category = "Weapon Properties")
USkeletalMeshComponent* WeaponMesh;
@ -157,14 +165,6 @@ private:
UPROPERTY(EditAnywhere)
EWeaponType WeaponType;
// Trace end with scatter
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float DistanceToSphere = 800.f;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float SphereRadius = 75.f;
public:
void SetWeaponState(EWeaponState State);