Use NiagaraSystem to spawn Particle Effects on bullet impact

This commit is contained in:
baz 2024-03-07 02:10:11 +00:00
parent cda5b81484
commit 76fb2475d2
6 changed files with 71 additions and 26 deletions

View File

@ -4,6 +4,7 @@
#include "EnemyAIController.h" #include "EnemyAIController.h"
#include "EnemyHealthComponent.h" #include "EnemyHealthComponent.h"
#include "InteractableComponent.h" #include "InteractableComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "BehaviorTree/BehaviorTree.h" #include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h" #include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BlackboardData.h" #include "BehaviorTree/BlackboardData.h"
@ -33,8 +34,9 @@ void AEnemyCharacter::OnFire()
CurrentWeapon->SetCurrentWeaponStatus(Firing); CurrentWeapon->SetCurrentWeaponStatus(Firing);
TArray<FHitResult> Hits = TArray<FHitResult>(); TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits); FVector direction = FVector::ZeroVector;
ProcessHits(Hits); CalculateHits(&Hits, &direction);
ProcessHits(Hits, direction);
CurrentWeapon->PlayFireSoundAtLocation(GetActorLocation()); CurrentWeapon->PlayFireSoundAtLocation(GetActorLocation());
@ -68,7 +70,7 @@ void AEnemyCharacter::PlayOnFireAnimations()
Super::PlayOnFireAnimations(); Super::PlayOnFireAnimations();
} }
void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits) void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits, FVector* dir)
{ {
// Set up randomness // Set up randomness
const int32 RandomSeed = FMath::Rand(); const int32 RandomSeed = FMath::Rand();
@ -108,7 +110,7 @@ void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits)
} }
} }
void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits) void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits, FVector dir)
{ {
for (FHitResult Hit : hits) for (FHitResult Hit : hits)
{ {
@ -117,11 +119,19 @@ void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits)
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn field actor // Spawn field actor
if (CurrentWeapon->GetFieldSystemActor())
{
FTransform transform; FTransform transform;
transform.SetLocation(Hit.ImpactPoint); transform.SetLocation(Hit.ImpactPoint);
auto field = GetWorld()->SpawnActor<AFieldSystemActor>(CurrentWeapon->GetFieldSystemActor(), transform, auto field = GetWorld()->SpawnActor<AFieldSystemActor>(CurrentWeapon->GetFieldSystemActor(), transform,
SpawnParameters); SpawnParameters);
if (field)
{
field->Destroy();
}
}
if (Hit.GetActor()) if (Hit.GetActor())
{ {
if (auto interactableComponent = Hit.GetActor()->GetComponentByClass<UInteractableComponent>()) if (auto interactableComponent = Hit.GetActor()->GetComponentByClass<UInteractableComponent>())
@ -135,6 +145,36 @@ void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits)
GetController(), this); GetController(), this);
} }
} }
auto staticMeshComponent = Hit.GetActor()->GetComponentByClass<UStaticMeshComponent>();
if (staticMeshComponent && !staticMeshComponent->IsSimulatingPhysics() && CurrentWeapon->GetDecalActor())
{
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
auto decalActor = GetWorld()->SpawnActor<ADecalActor>(CurrentWeapon->GetDecalActor(), transform,
SpawnParameters);
auto rot = Hit.ImpactNormal.Rotation();
rot.Roll += 90.0f;
rot.Yaw += 180.0f;
decalActor->SetActorRotation(rot);
}
if (staticMeshComponent && !staticMeshComponent->IsSimulatingPhysics() &&
CurrentWeapon->GetImpactParticleSystem())
{
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this,
CurrentWeapon->GetImpactParticleSystem(),
transform.GetLocation(),
dir.MirrorByVector(Hit.ImpactNormal).Rotation(),
FVector(1),
true);
}
} }
} }

View File

@ -52,9 +52,9 @@ protected:
private: private:
virtual void PlayOnFireAnimations() override; virtual void PlayOnFireAnimations() override;
virtual void CalculateHits(TArray<FHitResult>* hits) override; virtual void CalculateHits(TArray<FHitResult>* hits, FVector* dir) override;
virtual void ProcessHits(TArray<FHitResult> hits) override; virtual void ProcessHits(TArray<FHitResult> hits, FVector dir) override;
protected: protected:
virtual void OnDamaged() override; virtual void OnDamaged() override;

View File

@ -257,11 +257,11 @@ bool ANakatomiCharacter::GetCrouched()
return false; return false;
} }
void ANakatomiCharacter::CalculateHits(TArray<FHitResult>* hits) void ANakatomiCharacter::CalculateHits(TArray<FHitResult>* hits, FVector* dir)
{ {
} }
void ANakatomiCharacter::ProcessHits(TArray<FHitResult> hits) void ANakatomiCharacter::ProcessHits(TArray<FHitResult> hits, FVector dir)
{ {
} }

View File

@ -116,9 +116,9 @@ public:
bool GetCrouched(); bool GetCrouched();
protected: protected:
virtual void CalculateHits(TArray<FHitResult>* hits); virtual void CalculateHits(TArray<FHitResult>* hits, FVector* dir);
virtual void ProcessHits(TArray<FHitResult> hits); virtual void ProcessHits(TArray<FHitResult> hits, FVector dir);
virtual void PlayOnFireAnimations(); virtual void PlayOnFireAnimations();

View File

@ -14,6 +14,7 @@
#include "InteractableComponent.h" #include "InteractableComponent.h"
#include "NakatomiCMC.h" #include "NakatomiCMC.h"
#include "WeaponThrowable.h" #include "WeaponThrowable.h"
#include "NiagaraFunctionLibrary.h"
#include "GameFramework/CharacterMovementComponent.h" #include "GameFramework/CharacterMovementComponent.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1 #define COLLISION_WEAPON ECC_GameTraceChannel1
@ -318,7 +319,7 @@ void APlayerCharacter::SetWalkingCallback(const FInputActionInstance& Instance)
} }
} }
void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits) void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits, FVector* dir)
{ {
// Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace // Set up the collision query params, use the Weapon trace settings, Ignore the actor firing this trace
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator()); FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator());
@ -349,6 +350,7 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
TraceStart = GetRootComponent()->GetComponentLocation(); TraceStart = GetRootComponent()->GetComponentLocation();
FVector AimDir = CamHit.ImpactPoint - TraceStart; FVector AimDir = CamHit.ImpactPoint - TraceStart;
AimDir.Normalize(); AimDir.Normalize();
*dir = AimDir;
TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir); TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir);
// Calculate the hit results from the trace // Calculate the hit results from the trace
@ -371,7 +373,7 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
} }
} }
void APlayerCharacter::ProcessHits(TArray<FHitResult> hits) void APlayerCharacter::ProcessHits(TArray<FHitResult> hits, FVector dir)
{ {
for (FHitResult Hit : hits) for (FHitResult Hit : hits)
{ {
@ -437,11 +439,13 @@ void APlayerCharacter::ProcessHits(TArray<FHitResult> hits)
FTransform transform; FTransform transform;
transform.SetLocation(Hit.ImpactPoint); transform.SetLocation(Hit.ImpactPoint);
UGameplayStatics::SpawnEmitterAtLocation(this, UNiagaraFunctionLibrary::SpawnSystemAtLocation(this,
CurrentWeapon->GetImpactParticleSystem(), CurrentWeapon->GetImpactParticleSystem(),
transform.GetLocation(), transform.GetLocation(),
FRotator::ZeroRotator, dir.MirrorByVector(Hit.ImpactNormal).Rotation(),
FVector(1),
true); true);
} }
} }
@ -643,8 +647,9 @@ void APlayerCharacter::OnFire()
CurrentWeapon->SetCurrentWeaponStatus(Firing); CurrentWeapon->SetCurrentWeaponStatus(Firing);
TArray<FHitResult> Hits = TArray<FHitResult>(); TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits); FVector direction = FVector::ZeroVector;
ProcessHits(Hits); CalculateHits(&Hits, &direction);
ProcessHits(Hits, direction);
CurrentWeapon->DecrementAmmoCount(1); CurrentWeapon->DecrementAmmoCount(1);

View File

@ -243,9 +243,9 @@ public:
void SetIsThrowing(bool bIsThrowing); void SetIsThrowing(bool bIsThrowing);
protected: protected:
virtual void CalculateHits(TArray<FHitResult>* hits) override; virtual void CalculateHits(TArray<FHitResult>* hits, FVector* dir) override;
virtual void ProcessHits(TArray<FHitResult> hits) override; virtual void ProcessHits(TArray<FHitResult> hits, FVector dir) override;
virtual void PlayOnFireAnimations() override; virtual void PlayOnFireAnimations() override;