How can I get a particle to spawn in a specific location?

Hey! I am trying to get some barrel smoke into my game. What I have done is I am trying to spawn a particle at the same location where the projectile spawns. The problem is that for some reason, the particle seems to spawn in the middle of the level (always, it never changes spawn location). Anyone know why this could happen? The code worked perfectly earlier on when I was testing it. Highly appreciate any useful answers or input!

const FRotator SpawnRotation = GetControlRotation();
		// MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
		const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset);

		UWorld* const World = GetWorld();
		if (World != NULL)
			{
				wpAmmo--;
				FString debugAmmo = FString::FromInt(wpAmmo);
				if (SmokeClass != NULL)
					{
					World->SpawnActor<AOneShotParticle>(SmokeClass, SpawnLocation, SpawnRotation);
					}
				// spawn the projectile at the muzzle
				World->SpawnActor<AInstinctProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
				GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Black, debugAmmo);
			}

#Spawn Emitter Attached

Using an actor for a particle is inefficient and will cost you later down the line

Also, your code wont keep the particle attached if the muzzle moves while the particle is still playing, so it wont look quite right :slight_smile:

use this instead!

UGameplayStatics::SpawnEmitterAttached(
	ThePSC, //UParticleSystem*
	SkelMeshToAttachTo, 
	SocketName, 
	FVector(0,0,0), //relative offset 
	FRotator(0,0,0), //relative rotation
	EAttachLocation::KeepRelativeOffset, 
	true //auto delete on completion
);

:slight_smile:

Rama

Thank you for your answer! My Unreal Engine is experiencing some crashes and I don’t know why, but I will look into this once I get it fixed.

Sorry, but I had a small question regarding the SpawnEmitter. It works very well, but how can I deactivate it? I only wanted it to spawn particles for a second or so at a time. Would really appreciated a reply!

Not sure if you ever figured this out but I think the particle system itself can have a lifetime , and then setting that last value to true should destroy it.