I have kind of figured out how I can accomplish this using the code below. It vacuums the particles in greatly, but it won’t destroy them when I set their lifetime to zero.
public float vacuumForce = 5f;
public float vacuumRadius = 5f;
public float destructionDist = 0.2f;
public Collider2D triggerCollider;
private void Update()
{
ParticleSystem[] particleSystems = FindObjectsOfType<ParticleSystem>();
foreach (ParticleSystem particleSystem in particleSystems)
{
ApplyVacuumForceToParticles(particleSystem);
}
}
private void ApplyVacuumForceToParticles(ParticleSystem particleSystem)
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.main.maxParticles];
int numParticlesAlive = particleSystem.GetParticles(particles);
for (int i = 0; i < numParticlesAlive; i++)
{
Vector3 particleWorldPosition = particleSystem.transform.TransformPoint(particles[i].position);
if (triggerCollider.OverlapPoint(particleWorldPosition))
{
Vector3 vacuumPosition = transform.position;
Vector3 directionToVacuum = (vacuumPosition - particleWorldPosition).normalized;
float distanceToVacuum = Vector2.Distance(vacuumPosition, particleWorldPosition);
if (distanceToVacuum < vacuumRadius)
{
particles[i].velocity += directionToVacuum * vacuumForce * Time.deltaTime;
}
if (distanceToVacuum <= destructionDist)
{
particles[i].remainingLifetime = 0;
}
}
}
particleSystem.SetParticles(particles, numParticlesAlive);
}
You don’t really need a script for this. You can use a particle system’s force field component to suck particles towards it and when they collide with the collider that’s on the force field object they can automatically destroy themselves.