- Home /
[Shuriken] emitting particles at arbitrary locations
I have a particle system, which I want to emit at a series of randomly determined points all during the same frame. Through testing, I find that I can simply set pfx.transform.position to the location and the particles will emit there when I call pfx.Emit(n).
However, When trying to make several particles at once in different locations, looping through several times and changing the pfx position before a call to pfx.Emit(1) does not work - they all emit in the first location set. Weird.
Here's the code I'm using:
// go is a game object var from earlier
Mesh m = go.GetComponent<MeshFilter>().mesh;
int cnt = m.vertices.Length;
int numParts = 10;
for (int i = 0; i < numParts; ++i)
{
Vector3 vpos = m.vertices[(int)(math.FRand() * cnt)];
// p is a ParticleSystem object
p.transform.position = go.transform.TransformPoint(vpos);
Debug.Log("vpos " + vpos + " tranforms into " + go.transform.TransformPoint(vpos));
p.Emit(1);
}
The debug I have in there shows me that the transformed positions are all correct, but only the first call to p.transform.position = go.transform.TransformPoint(vpos) seems to have any effect. I think this is perhaps something to do with needing to update a frame first? I've used similar code before to make trails on moving objects, but they emit in one place per frame, not several places at once.
Has anyone experienced this or know a way around it?
Let me see if I understand you correctly. You want to use the same particle system to emit particles at different positions within the same frame? Not sure if that's possible.
Have you tried changing the position within consecutive frames? Don't think the player would be able to see the difference.
Consecutive frames works fine, just not the same frame. It's very strange that the first call to set the position of the emitter works, but subsequent calls don't seem to move it until the next frame.
Although thinking about it, it's probably the last position call that 'works'. I guess Unity doesn't update new positions until the internal frame logic fires. What a nuisance.
Answer by foxor · Nov 13, 2012 at 07:59 PM
That sounds like a bug with Emit to me. Here's how you can work around it:
p.Emit(numParts)
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[numParts];
if (particleSystem.GetParticles(particles) < numParts) {
Debug.Log("Hey!");
return;
}
for (int i = 0; i < numCount; i++) {
particles[i].position = ...
}
It doesn't work if you're using that emitter for other things, but it should work around your problem.
Thanks foxor but you're a touch too late - they have just fixed it in Unity4. You can now emit in specific locations, or by passing in an array of Particles. Have a tick anyway - it's still useful for v3 users.
Yeah, I was looking for a different problem in unity4, but I figured I could at least answer this one.