- Home /
Spawn particles from a position
Is it possible to spawn particles from defined positions within a shuriken particle system?
I want to spawn the particles, and let them use the existing settings of the particle ystem such as start lifetime, size, force with all random values and details; just like a randomly spawned particle in that particle system
Any ideas?
Answer by mptp · Oct 09, 2014 at 02:02 PM
This is an old question, but thought I'd answer here for posterity:
What you want to do is have a particle system, and each frame add some new particles that you create through code to its list of particles. I put a complete demo here which should be pretty clear.
ParticleSystem yourParts = GameObject.Find("yourParts").GetComponent<ParticleSystem>();
int partsPerFrame = 20;
void Update() {
//Get the particles already in the system
ParticleSystem.Particle[] particles_original = new ParticleSystem.Particle[yourParts.particleCount];
yourParts.GetParticles (particles_original);
//Make an array of particles to add
ParticleSystem.Particle[] particles_new = new ParticleSystem.Particle[partsPerFrame];
Vector3 spawnPos;
for(int i = 0; i < partsPerFrame; i++) {
//Here I'm setting their position randomly, but you could pass any position you want here
spawnPos = new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f));
particles_new[i] = makePart(spawnPos);
}
//Combine the currently existing particles with the new ones you just created
ParticleSystem.Particle[] particles_final = new ParticleSystem.Particle[particles_original.Length + particles_new.Length];
particles_original.CopyTo(particles_final, 0);
particles_new.CopyTo(particles_final, particles_original.Length);
//And put them into the particle system
yourParts.SetParticles (particles_final, particles_final.Length);
}
//This initialises all of the ParticleSystem.Particle attributes whenever you make one.
//Each attribute is assigned a variable that I store as class variables. I didn't write them in because it would end up too messy looking.
ParticleSystem.Particle makePart(Vector3 position) {
ParticleSystem.Particle r = new ParticleSystem.Particle();
r.angularVelocity = part_angularVelocity;
r.color = part_color;
r.lifetime = part_lifetime;
r.position = position;
r.rotation = part_rotation;
r.size = part_size;
r.startLifetime = part_startLifetime;
r.velocity = part_velocity;
return r;
}
Answer by petersvp · Nov 08, 2015 at 02:47 AM
@zettam, There is another, more efficient and very easy way:
Make your particle system simulate in World Space, and make special object that only holds your particle system.
Then, every time you want to burst, just move the emitter into the position you want, and burst there.
If you want to burst in two or more places at the same time, make array / list op positions, and in the update() on your script, teleport-emit() in all these positions.
Just don't forget to set the system to simulate in World space, because in local space, particles will be parented to the object :)
Answer by Cherno · Nov 18, 2013 at 01:20 AM
Wouldn't this just mean that you instantiate a prefab particle system at the required location that has the required values, but just one single particle?
Answer by cobertos · May 08, 2020 at 02:02 AM
You can use ParticleSystem.Emit()
and provide overrides for the .position
property
https://docs.unity3d.com/ScriptReference/ParticleSystem.Emit.html
Your answer
Follow this Question
Related Questions
Shuriken Collision Detection 0 Answers
Adjust particles to simluate real world 0 Answers
Shuriken Particle Collisions - Collider shape? 0 Answers
Get all particles within trigger collider? 1 Answer
How do I Update the transform of Instantiate prefab? 2 Answers