Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote a script to manage my Particle System emission. The starting lifespan is set to infinite so that the particle never dies until it reaches a specific collider.

here's the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms;

public class water : MonoBehaviour
{
    private ParticleSystem ps;
    [Range(0.0f, 100f)]
    public int maxParticles;
    public float particleLife;
    public bool neverDies;

    public List<ParticleCollisionEvent> collisionEvents;



    void Start()
    {
        ps = GetComponent<ParticleSystem>();
        collisionEvents = new List<ParticleCollisionEvent>();
    }

    void OnParticleCollision(GameObject other)
    {
        int numCollisionEvents = ps.GetCollisionEvents(other, collisionEvents);
        int i = 0;

        while (i < numCollisionEvents)
       {
            // here i should destroy the particles hitting the gameobject
            i++;
        }
    }


void Update()
{
    Debug.Log("Particles Alive Count: " + GetAliveParticles());

    ps.Emit(maxParticles);
    var main = ps.main;
    main.maxParticles = Mathf.RoundToInt(maxParticles);
    main.startLifetime = particleLife;

    if (neverDies)
    {
        particleLife = float.PositiveInfinity;
    }
    else
    {
        particleLife = 5f;
    }
}



int GetAliveParticles()
{
    ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ps.particleCount];
    return ps.GetParticles(particles);
}


What I have tried:

I didn't find any method to destroy a specific particle in the Unity Manual.


Unity - Scripting API: MainModule[^]
Posted
Comments
[no name] 12-Jan-21 14:22pm    
Did you put a debug break point in your "collision detector" to see if it is getting called? Is it hooked up? (Didn't see it).
Member 15044276 12-Jan-21 14:28pm    
no i didn't because i'm one step before. i don't even know what command to issue to destroy a particle `particle.Destroy(something like that)`
[no name] 13-Jan-21 0:07am    
In the absence of any special effects, you free the particle / object on collision. You could "fade" by changing opacity before stopping / releasing for example. Destroying is the reverse of creating (objects).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900