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 "EnemyHealthComponent.h"
#include "InteractableComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BlackboardData.h"
@ -33,8 +34,9 @@ void AEnemyCharacter::OnFire()
CurrentWeapon->SetCurrentWeaponStatus(Firing);
TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits);
ProcessHits(Hits);
FVector direction = FVector::ZeroVector;
CalculateHits(&Hits, &direction);
ProcessHits(Hits, direction);
CurrentWeapon->PlayFireSoundAtLocation(GetActorLocation());
@ -68,7 +70,7 @@ void AEnemyCharacter::PlayOnFireAnimations()
Super::PlayOnFireAnimations();
}
void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits)
void AEnemyCharacter::CalculateHits(TArray<FHitResult>* hits, FVector* dir)
{
// Set up randomness
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)
{
@ -117,10 +119,18 @@ void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits)
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn field actor
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
auto field = GetWorld()->SpawnActor<AFieldSystemActor>(CurrentWeapon->GetFieldSystemActor(), transform,
SpawnParameters);
if (CurrentWeapon->GetFieldSystemActor())
{
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
auto field = GetWorld()->SpawnActor<AFieldSystemActor>(CurrentWeapon->GetFieldSystemActor(), transform,
SpawnParameters);
if (field)
{
field->Destroy();
}
}
if (Hit.GetActor())
{
@ -135,6 +145,36 @@ void AEnemyCharacter::ProcessHits(TArray<FHitResult> hits)
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:
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:
virtual void OnDamaged() override;

View File

@ -257,11 +257,11 @@ bool ANakatomiCharacter::GetCrouched()
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();
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();

View File

@ -14,6 +14,7 @@
#include "InteractableComponent.h"
#include "NakatomiCMC.h"
#include "WeaponThrowable.h"
#include "NiagaraFunctionLibrary.h"
#include "GameFramework/CharacterMovementComponent.h"
#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
FCollisionQueryParams TraceParams(SCENE_QUERY_STAT(WeaponTrace), true, GetInstigator());
@ -349,6 +350,7 @@ void APlayerCharacter::CalculateHits(TArray<FHitResult>* hits)
TraceStart = GetRootComponent()->GetComponentLocation();
FVector AimDir = CamHit.ImpactPoint - TraceStart;
AimDir.Normalize();
*dir = AimDir;
TraceStart = TraceStart + AimDir * ((GetInstigator()->GetActorLocation() - TraceStart) | AimDir);
// 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)
{
@ -436,12 +438,14 @@ void APlayerCharacter::ProcessHits(TArray<FHitResult> hits)
{
FTransform transform;
transform.SetLocation(Hit.ImpactPoint);
UGameplayStatics::SpawnEmitterAtLocation(this,
CurrentWeapon->GetImpactParticleSystem(),
transform.GetLocation(),
FRotator::ZeroRotator,
true);
UNiagaraFunctionLibrary::SpawnSystemAtLocation(this,
CurrentWeapon->GetImpactParticleSystem(),
transform.GetLocation(),
dir.MirrorByVector(Hit.ImpactNormal).Rotation(),
FVector(1),
true);
}
}
@ -643,8 +647,9 @@ void APlayerCharacter::OnFire()
CurrentWeapon->SetCurrentWeaponStatus(Firing);
TArray<FHitResult> Hits = TArray<FHitResult>();
CalculateHits(&Hits);
ProcessHits(Hits);
FVector direction = FVector::ZeroVector;
CalculateHits(&Hits, &direction);
ProcessHits(Hits, direction);
CurrentWeapon->DecrementAmmoCount(1);

View File

@ -243,9 +243,9 @@ public:
void SetIsThrowing(bool bIsThrowing);
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;