- Home /
Destroy Particles if they are outside their emitter
Hi, I want to destroy the particles which, when the emitter moves around particles float off into the distance.
This is what ive got so far but it dont work correctly (c#)
void OnTriggerExit(Collider col)
{
UnityEngine.Object.Destroy(col.collider);
}
For game speed, I don't think particles interact with the physics engine like regular gameObjects (like OnTrigger*.)
There's a special OnParticleCollision, but it just says "one of your guys hit me", and sounds like it won't fire on triggers.
get/setParticles allows you to see them all. In theory you could hand-check distance in there, if you can find an example.
$$anonymous$$ight be easier just to redo the system (making more particles that live less, so they can't get too far away, inherit more velocity, use local space ... .)
Answer by justinspice · Jul 22, 2021 at 04:01 AM
you could also just use the trigger event in the particle system and have them killed if they leave a volume
Answer by ShawnFeatherly · Sep 17, 2015 at 02:11 AM
I did something similar by destroying particles that are a certain distance from the emitter:
using System.Linq;
/// <summary>
/// Destroys particles that are farther than DistanceToDestroy from the particlesystem emitter
/// </summary>
[RequireComponent(typeof(ParticleSystem))]
public class ParticleMaxDistance : MonoBehaviour
{
public float DistanceToDestroy;
private ParticleSystem cachedSystem;
private Vector3 staticPosition;
void Start()
{
cachedSystem = this.GetComponent<ParticleSystem>();
staticPosition = this.transform.position;
}
void Update()
{
ParticleSystem.Particle[] ps = new ParticleSystem.Particle[cachedSystem.particleCount];
cachedSystem.GetParticles(ps);
// keep only particles that are within DistanceToDestroy
var distanceParticles = ps.Where(p => Vector3.Distance(staticPosition, p.position) < DistanceToDestroy).ToArray();
cachedSystem.SetParticles(distanceParticles, distanceParticles.Length);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120654im_/https://answers.unity.com/themes/thub/images/avi.jpg)