- Home /
[Shuriken] Direct particles to a point
Hi all,
I'm trying to use a particle system to create some kind of "collect" effect inside the game. When the player collects an item, I want to emit several particles, which keep flying around the place I collected the item, and after a while, they go flying to the center of the box in the inventory.
More or less I know how to stop them after a few seconds, but I don't know how to thrown them to the inventory later.
Thanks!
Answer by rutter · Nov 05, 2013 at 08:02 PM
It's possible (but laborious) to take direct control of the ParticleSystem's particles:
public class ImplodingEmitter : MonoBehaviour {
void Start() {
StartCoroutine(Implode());
}
IEnumerator Implode() {
if (!particleSystem.isPlaying) {
particleSystem.Play();
}
//let the system do it's thing for a bit
yield return new WaitForSeconds(3f);
//stop emission
particleSystem.emissionRate = 0f;
//allocate reference array
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.particleCount];
//this loop executes over several frames
// - get particle list
// - update each particle's position
// - set particle list
// - wait one frame
for (float t = 0f; t < 1f; t += 0.1f) {
int count = particleSystem.GetParticles(particles);
for (int i=0; i<count; i++) {
particles[i].position = Vector3.Lerp(particles[i].position, transform.position, t);
}
particleSystem.SetParticles(particles, count);
yield return null;
}
//once loop is finished, clear particles
particleSystem.Clear();
}
}
Other option, and honestly it might be easier, is to just create your own GameObjects and move those. Each GameObject could have an emitter attached (say, a single particle set to simulation space "local"), or could just be a quad that happens to look an awful lot like a particle.
Hey, just wanted to say this is a very useful answer and script. Just implemented it for my game and it still works great. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613121313im_/https://answers.unity.com/themes/thub/images/avi.jpg)