kill particles going outside certain volume

Hello,

I have a particle system with a box emission shape.
It works great, but I do not want the particles to go outside the box shape.
I want to kill any particle immediately if it goes outside the box border.
The main reason for this is to increase performance.
Any way to achieve that?

Thank you for advance, any help is greatly appreciated.

Unless you have a huge number of particles, I doubt eliminating particles will be a performance boost. Even with a huge number of particles, the processing to eliminate particles may cost more than is saved. Since I don’t know for sure, there is some code that does what you ask. It use a Bounds to define the area. The size of the bounds (extents) should be set in the inspector. Note that it is more efficient to define a spherical space rather than a box.

#pragma strict

public var bounds : Bounds;

private var ps : ParticleSystem;
private var particles : ParticleSystem.Particle[];

function Start() {
	ps = particleSystem;
	particles = new ParticleSystem.Particle[ps.maxParticles];
}

function Update() {
	if (ps.simulationSpace == ParticleSystemSimulationSpace.World) {
		bounds.center = transform.position;  
	}
	var count = ps.GetParticles(particles);
	for (var i = 0; i < count; i++) {
		if (!bounds.Contains(particles*.position)) {*

_ particles*.lifetime = -1.0;_
_
}_
_
}_
_
ps.SetParticles(particles, count);_
_
}*_