- Home /
Question by
ayaraee · Sep 14, 2016 at 02:52 PM ·
c#particlesparticlesystemparticle systemparticle
Destroy particles based on bounds not lifetime
Is it possible to bound a particle system? I.e. have it only run from x1 to x2, and y1 to y2. Any particle that exits the bounds is destroyed. Preferably done without collisions as they can be very expensive for a lot of particles and unnecessary So far I have this
[RequireComponent(typeof(ParticleSystem))]
public class BoundParticles : MonoBehaviour
{
public float MaxHeight;
private ParticleSystem _ps;
[Inject]
public void Initialize(Settings settings)
{
_ps = GetComponent<ParticleSystem>();
}
void Update()
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[_ps.particleCount];
int num = _ps.GetParticles(particles);
while (--num >= 0)
{
if (particles[num].position.z > MaxHeight)
{
particles[num].lifetime = 0;
}
}
_ps.SetParticles(particles, particles.Length);
}
Is there a more efficient way to clear the particles based on position?
Comment