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())
{
bCanFire = false;
ServerFire(HitTarget);
LocalFire(HitTarget);
if (PrimaryWeapon)
{
CrosshairShootingFactor = .75f;
switch (PrimaryWeapon->FireType)
{
case EFireType::EFT_HitScan:
FireHitScanWeapon();
break;
case EFireType::EFT_Projectile:
FireProjectileWeapon();
break;
case EFireType::EFT_Shotgun:
FireShotgun();
break;
}
}
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()
{
if (PrimaryWeapon == nullptr || Character == nullptr) return;

View File

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

View File

@ -6,7 +6,6 @@
#include "Blaster/Character/BlasterCharacter.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetMathLibrary.h"
#include "Particles/ParticleSystemComponent.h"
void AHitScanWeapon::Fire(const FVector& HitTarget)
@ -78,7 +77,7 @@ void AHitScanWeapon::WeaponTraceHit(const FVector& TraceStart, const FVector& Hi
UWorld* World = GetWorld();
if (World)
{
FVector End = bUseScatter ? TraceEndWithScatter(TraceStart, HitTarget) : TraceStart + (HitTarget - TraceStart) * 1.25f;
FVector End = TraceStart + (HitTarget - TraceStart) * 1.25f;
World->LineTraceSingleByChannel(
OutHit,
TraceStart,
@ -90,6 +89,9 @@ void AHitScanWeapon::WeaponTraceHit(const FVector& TraceStart, const FVector& Hi
{
BeamEnd = OutHit.ImpactPoint;
}
DrawDebugSphere(GetWorld(), BeamEnd, 16.f, 12, FColor::Orange, true);
if (BeamParticles)
{
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()
public:
virtual void Fire(const FVector& HitTarget) override;
protected:
FVector TraceEndWithScatter(const FVector& TraceStart, const FVector& HitTarget);
void WeaponTraceHit(const FVector& TraceStart, const FVector& HitTarget, FHitResult& OutHit);
UPROPERTY(EditAnywhere)
@ -43,15 +40,4 @@ private:
UPROPERTY(EditAnywhere)
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/WidgetComponent.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/KismetMathLibrary.h"
#include "Net/UnrealNetwork.h"
AWeapon::AWeapon()
@ -292,3 +293,31 @@ void AWeapon::AddAmmo(int32 Amount)
Ammo = FMath::Clamp(Ammo - Amount, 0, MagCapacity);
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")
};
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()
class BLASTER_API AWeapon : public AActor
{
@ -32,6 +42,7 @@ public:
virtual void Fire(const FVector& HitTarget);
void Dropped();
void AddAmmo(int32 Amount);
FVector TraceEndWithScatter(const FVector& HitTarget);
// Textures for the weapon crosshairs
@ -72,6 +83,13 @@ public:
void EnableCustomDepth(bool bEnabled);
bool bDestroyWeapon = false;
UPROPERTY(EditAnywhere)
EFireType FireType;
UPROPERTY(EditAnywhere, Category = "Weapon Scatter")
bool bUseScatter = false;
protected:
virtual void BeginPlay() override;
@ -140,6 +158,14 @@ 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);
FORCEINLINE USphereComponent* GetAreaSphere() const { return AreaSphere; }