Hit particles spawning at 0,0,0

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();
}

Could you show the code that spawns the projectile?


void ABasePawn::Fire()
{
	auto ProjectileFired = GetWorld()->SpawnActor<AProjectile>(ProjectileClass,
	ProjectileSpawnPoint->GetComponentLocation(),
	ProjectileSpawnPoint->GetComponentRotation());

	ProjectileFired->SetOwner(this);
}

Never mind i just now noticed i had “Projectile->DestroyComponent();” at the top of ProjectileOnHit for some reason, it was fixed after i deleted it. Sorry about that.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms