76 - Projectile Hit Events

This commit is contained in:
Kingsmedia 2022-05-05 15:02:47 +02:00
parent 99de487299
commit ffa9bfc84c
3 changed files with 38 additions and 2 deletions

View File

@ -6,6 +6,7 @@
#include "Components/BoxComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
AProjectile::AProjectile()
{
@ -39,6 +40,31 @@ void AProjectile::BeginPlay()
EAttachLocation::KeepWorldPosition
);
}
if (HasAuthority())
{
CollisionBox->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
}
}
void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
Destroy();
}
void AProjectile::Destroyed()
{
Super::Destroyed();
if (ImpactParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, GetActorTransform());
}
if (ImpactSound)
{
UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation());
}
}
void AProjectile::Tick(float DeltaTime)

View File

@ -18,8 +18,12 @@ public:
protected:
virtual void BeginPlay() override;
public:
UFUNCTION()
virtual void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
virtual void Destroyed() override;
private:
UPROPERTY(EditAnywhere)
class UBoxComponent* CollisionBox;
@ -31,4 +35,10 @@ public:
class UParticleSystem* Tracer;
class UParticleSystemComponent* TracerComponent;
UPROPERTY(EditAnywhere)
UParticleSystem* ImpactParticles;
UPROPERTY(EditAnywhere)
class USoundCue* ImpactSound;
};