131 - Spawning Rocket Trails

This commit is contained in:
Kingsmedia 2022-05-18 19:20:18 +02:00
parent 34afe73a63
commit b247adc32d
7 changed files with 119 additions and 13 deletions

View File

@ -8,7 +8,7 @@ public class Blaster : ModuleRules
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "MultiplayerSessions", "OnlineSubsystem", "OnlineSubsystemSteam" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Niagara" });
PrivateDependencyModuleNames.AddRange(new string[] { });

View File

@ -25,12 +25,18 @@ protected:
UPROPERTY(EditAnywhere)
float Damage = 20.f;
private:
UPROPERTY(EditAnywhere)
class UBoxComponent* CollisionBox;
class UParticleSystem* ImpactParticles;
UPROPERTY(EditAnywhere)
class USoundCue* ImpactSound;
UPROPERTY(EditAnywhere)
class UBoxComponent* CollisionBox;
private:
UPROPERTY(VisibleAnywhere)
class UProjectileMovementComponent* ProjectileMovementComponent;
@ -39,10 +45,4 @@ private:
UPROPERTY()
class UParticleSystemComponent* TracerComponent;
UPROPERTY(EditAnywhere)
UParticleSystem* ImpactParticles;
UPROPERTY(EditAnywhere)
class USoundCue* ImpactSound;
};

View File

@ -4,6 +4,11 @@
#include "ProjectileRocket.h"
#include "Kismet/GameplayStatics.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.h"
#include "Sound/SoundCue.h"
AProjectileRocket::AProjectileRocket()
{
@ -12,10 +17,59 @@ AProjectileRocket::AProjectileRocket()
RocketMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void AProjectileRocket::Destroyed()
{
}
void AProjectileRocket::BeginPlay()
{
Super::BeginPlay();
if (!HasAuthority())
{
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectileRocket::OnHit);
}
if (TrailSystem)
{
TrailSystemComponent = UNiagaraFunctionLibrary::SpawnSystemAttached(
TrailSystem,
GetRootComponent(),
FName(),
GetActorLocation(),
GetActorRotation(),
EAttachLocation::KeepWorldPosition,
false
);
}
if (ProjectileLoop && LoopingSoundAttenuation)
{
ProjectileLoopComponent = UGameplayStatics::SpawnSoundAttached(
ProjectileLoop,
GetRootComponent(),
FName(),
GetActorLocation(),
EAttachLocation::KeepWorldPosition,
false,
1.f,
1.f,
0.f,
LoopingSoundAttenuation,
static_cast<USoundConcurrency*>(nullptr),
false
);
}
}
void AProjectileRocket::DestroyTimerFinished()
{
Destroy();
}
void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
APawn* FiringPawn = GetInstigator();
if (FiringPawn)
if (FiringPawn && HasAuthority())
{
AController* FiringController = FiringPawn->GetController();
if (FiringController)
@ -35,5 +89,36 @@ void AProjectileRocket::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor,
);
}
}
Super::OnHit(HitComp, OtherActor, OtherComp, NormalImpulse, Hit);
GetWorldTimerManager().SetTimer(
DestroyTimer,
this,
&AProjectileRocket::DestroyTimerFinished,
DestroyTime
);
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
if (RocketMesh)
{
RocketMesh->SetVisibility(false);
}
if (CollisionBox)
{
CollisionBox->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
if (TrailSystemComponent && TrailSystemComponent->GetSystemInstanceController().IsValid())
{
TrailSystemComponent->GetSystemInstanceController()->Deactivate();
}
if (ProjectileLoopComponent && ProjectileLoopComponent->IsPlaying())
{
ProjectileLoopComponent->Stop();
}
}

View File

@ -15,13 +15,34 @@ class BLASTER_API AProjectileRocket : public AProjectile
GENERATED_BODY()
public:
AProjectileRocket();
virtual void Destroyed() override;
protected:
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) override;
virtual void BeginPlay() override;
void DestroyTimerFinished();
UPROPERTY(EditAnywhere)
class UNiagaraSystem* TrailSystem;
UPROPERTY()
class UNiagaraComponent* TrailSystemComponent;
UPROPERTY(EditAnywhere)
USoundCue* ProjectileLoop;
UPROPERTY()
UAudioComponent* ProjectileLoopComponent;
UPROPERTY(EditAnywhere)
USoundAttenuation* LoopingSoundAttenuation;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* RocketMesh;
FTimerHandle DestroyTimer;
UPROPERTY(EditAnywhere)
float DestroyTime = 3.f;
};