when the projectile gets destroyed, the hit particles always spawn at the center of the world and i cant figure out why, this is the projectile.cpp file:
#include "Projectile.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "GameFramework/DamageType.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AProjectile::AProjectile()
{
Projectile = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile"));
RootComponent = Projectile;
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovement"));
ProjectileMovement->InitialSpeed = 50.f;
ProjectileMovement->MaxSpeed = 50.f;
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
Projectile->OnComponentHit.AddDynamic(this,&AProjectile::ProjectileOnHit);
}
// Called every frame
void AProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AProjectile::ProjectileOnHit(UPrimitiveComponent* HitComp, AActor* OtherActor,UPrimitiveComponent* OtherComp,FVector NormalImpulse, const FHitResult& Hit)
{
Projectile->DestroyComponent();
auto MyOwner = GetOwner();
if (MyOwner == nullptr)
{
UE_LOG(LogTemp, Warning,TEXT("error"));
Destroy();
return;
}
auto MyOwnerInstigator = MyOwner->GetInstigatorController();
auto DamageTypeClass = UDamageType::StaticClass();
if (OtherActor && OtherActor != this && OtherActor != MyOwner)
{
UGameplayStatics::ApplyDamage(OtherActor,Damage,MyOwnerInstigator,this,DamageTypeClass);
if (HitParticles)
{
UGameplayStatics::SpawnEmitterAtLocation(this,
HitParticles,
GetActorLocation(),
GetActorRotation());
UE_LOG(LogTemp, Warning,TEXT("Actor Location is %s"),*GetActorLocation().ToString());
}
}
Destroy();
}