173 - Replicating Scatter

This commit is contained in:
Kingsmedia 2022-05-26 14:03:51 +02:00
parent 1585198be3
commit e107458690
6 changed files with 96 additions and 41 deletions

View File

@ -102,16 +102,47 @@ void UCombatComponent::Fire()
if (CanFire()) if (CanFire())
{ {
bCanFire = false; bCanFire = false;
ServerFire(HitTarget);
LocalFire(HitTarget);
if (PrimaryWeapon) if (PrimaryWeapon)
{ {
CrosshairShootingFactor = .75f; CrosshairShootingFactor = .75f;
switch (PrimaryWeapon->FireType)
{
case EFireType::EFT_HitScan:
FireHitScanWeapon();
break;
case EFireType::EFT_Projectile:
FireProjectileWeapon();
break;
case EFireType::EFT_Shotgun:
FireShotgun();
break;
}
} }
StartFireTimer(); StartFireTimer();
} }
} }
void UCombatComponent::FireProjectileWeapon()
{
LocalFire(HitTarget);
ServerFire(HitTarget);
}
void UCombatComponent::FireHitScanWeapon()
{
if (PrimaryWeapon)
{
HitTarget = PrimaryWeapon->bUseScatter ? PrimaryWeapon->TraceEndWithScatter(HitTarget) : HitTarget;
LocalFire(HitTarget);
ServerFire(HitTarget);
}
}
void UCombatComponent::FireShotgun()
{
}
void UCombatComponent::StartFireTimer() void UCombatComponent::StartFireTimer()
{ {
if (PrimaryWeapon == nullptr || Character == nullptr) return; if (PrimaryWeapon == nullptr || Character == nullptr) return;

View File

@ -60,6 +60,9 @@ protected:
void OnRep_SecondaryWeapon(); void OnRep_SecondaryWeapon();
void Fire(); void Fire();
void FireProjectileWeapon();
void FireHitScanWeapon();
void FireShotgun();
UFUNCTION(Server, Reliable) UFUNCTION(Server, Reliable)
void ServerFire(const FVector_NetQuantize& TraceHitTarget); void ServerFire(const FVector_NetQuantize& TraceHitTarget);

View File

@ -6,7 +6,6 @@
#include "Blaster/Character/BlasterCharacter.h" #include "Blaster/Character/BlasterCharacter.h"
#include "Engine/SkeletalMeshSocket.h" #include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "Particles/ParticleSystemComponent.h" #include "Particles/ParticleSystemComponent.h"
void AHitScanWeapon::Fire(const FVector& HitTarget) void AHitScanWeapon::Fire(const FVector& HitTarget)
@ -78,7 +77,7 @@ void AHitScanWeapon::WeaponTraceHit(const FVector& TraceStart, const FVector& Hi
UWorld* World = GetWorld(); UWorld* World = GetWorld();
if (World) if (World)
{ {
FVector End = bUseScatter ? TraceEndWithScatter(TraceStart, HitTarget) : TraceStart + (HitTarget - TraceStart) * 1.25f; FVector End = TraceStart + (HitTarget - TraceStart) * 1.25f;
World->LineTraceSingleByChannel( World->LineTraceSingleByChannel(
OutHit, OutHit,
TraceStart, TraceStart,
@ -90,6 +89,9 @@ void AHitScanWeapon::WeaponTraceHit(const FVector& TraceStart, const FVector& Hi
{ {
BeamEnd = OutHit.ImpactPoint; BeamEnd = OutHit.ImpactPoint;
} }
DrawDebugSphere(GetWorld(), BeamEnd, 16.f, 12, FColor::Orange, true);
if (BeamParticles) if (BeamParticles)
{ {
UParticleSystemComponent* Beam = UGameplayStatics::SpawnEmitterAtLocation( UParticleSystemComponent* Beam = UGameplayStatics::SpawnEmitterAtLocation(
@ -106,25 +108,3 @@ void AHitScanWeapon::WeaponTraceHit(const FVector& TraceStart, const FVector& Hi
} }
} }
} }
FVector AHitScanWeapon::TraceEndWithScatter(const FVector& TraceStart, const FVector& HitTarget)
{
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;
/*
DrawDebugSphere(GetWorld(), SphereCenter, SphereRadius, 12, FColor::Red, true);
DrawDebugSphere(GetWorld(), ToEndLoc, 4.f, 12, FColor::Blue, true);
DrawDebugLine(
GetWorld(),
TraceStart,
FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size()),
FColor::Cyan,
true
);
*/
return FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size());
}

View File

@ -15,12 +15,9 @@ class BLASTER_API AHitScanWeapon : public AWeapon
GENERATED_BODY() GENERATED_BODY()
public: public:
virtual void Fire(const FVector& HitTarget) override; virtual void Fire(const FVector& HitTarget) override;
protected: protected:
FVector TraceEndWithScatter(const FVector& TraceStart, const FVector& HitTarget);
void WeaponTraceHit(const FVector& TraceStart, const FVector& HitTarget, FHitResult& OutHit); void WeaponTraceHit(const FVector& TraceStart, const FVector& HitTarget, FHitResult& OutHit);
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
@ -43,15 +40,4 @@ private:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
USoundCue* FireSound; USoundCue* FireSound;
// Trace end with scatter
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float DistanceToSphere = 800.f;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
float SphereRadius = 75.f;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
bool bUseScatter = false;
}; };

View File

@ -10,6 +10,7 @@
#include "Components/SphereComponent.h" #include "Components/SphereComponent.h"
#include "Components/WidgetComponent.h" #include "Components/WidgetComponent.h"
#include "Engine/SkeletalMeshSocket.h" #include "Engine/SkeletalMeshSocket.h"
#include "Kismet/KismetMathLibrary.h"
#include "Net/UnrealNetwork.h" #include "Net/UnrealNetwork.h"
AWeapon::AWeapon() AWeapon::AWeapon()
@ -292,3 +293,31 @@ void AWeapon::AddAmmo(int32 Amount)
Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity); Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity);
SetHUDAmmo(); SetHUDAmmo();
} }
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;
/*
DrawDebugSphere(GetWorld(), SphereCenter, SphereRadius, 12, FColor::Red, true);
DrawDebugSphere(GetWorld(), ToEndLoc, 4.f, 12, FColor::Blue, true);
DrawDebugLine(
GetWorld(),
TraceStart,
FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size()),
FColor::Cyan,
true
);
*/
return FVector(TraceStart + ToEndLoc * TRACE_LENGTH / ToEndLoc.Size());
}

View File

@ -18,6 +18,16 @@ enum class EWeaponState : uint8
EWS_MAX UMETA(DisplayName = "DefaultMAX") EWS_MAX UMETA(DisplayName = "DefaultMAX")
}; };
UENUM(BlueprintType)
enum class EFireType: uint8
{
EFT_HitScan UMETA(DisplayName = "HitScan Weapon"),
EFT_Projectile UMETA(DisplayName = "Projectile Weapon"),
EFT_Shotgun UMETA(DisplayName = "Shotgun Weapon"),
EFT_Max UMETA(DisplayName = "DefaultMax")
};
UCLASS() UCLASS()
class BLASTER_API AWeapon : public AActor class BLASTER_API AWeapon : public AActor
{ {
@ -32,6 +42,7 @@ public:
virtual void Fire(const FVector& HitTarget); virtual void Fire(const FVector& HitTarget);
void Dropped(); void Dropped();
void AddAmmo(int32 Amount); void AddAmmo(int32 Amount);
FVector TraceEndWithScatter(const FVector& HitTarget);
// Textures for the weapon crosshairs // Textures for the weapon crosshairs
@ -72,6 +83,13 @@ public:
void EnableCustomDepth(bool bEnabled); void EnableCustomDepth(bool bEnabled);
bool bDestroyWeapon = false; bool bDestroyWeapon = false;
UPROPERTY(EditAnywhere)
EFireType FireType;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
bool bUseScatter = false;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -139,6 +157,14 @@ private:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
EWeaponType WeaponType; 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: public:
void SetWeaponState(EWeaponState State); void SetWeaponState(EWeaponState State);