x


How to destroy / hide a single particle?

I tried using ParticleSystem.SetParticles to change the lifetime of a particle, but it reset the other particles. Is there a way to simply destroy / hide / move one particle without resetting the system?

more ▼

asked Jun 22 '12 at 08:03 PM

Essential gravatar image

Essential
480 48 65 79

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

What exactly did you do? As with the old system, I would guess you first have to get all current particles with ParticleSystem.GetParticles, then remove the particle you want from that array somehow, and then reassign that new array back to the ParticleSystem (but read on).

In the old particle system (ParticleEmitter), instead of removing the particle physically from the array, it was enough to set its energy to 0. This way the ParticleEmitter will take care of the rest, recognize the particle als being dead/finished, and remove it itself from its internal particle array. I would assume you can do the same with Shuriken?

EDIT: I tested this now for myself. The Shuriken works the same as the old system, you can modify/remove/add individual particles. BUT the API call to get the existing particles is just plain weird and stupid, I have no idea why anyone would program it in such a way: Instead of simply returning the particles array, which you then can modify, you must provide your own array, which is passed to GetParticles(). After this call returns, your particles array has been filled with the information of the existing ParticleSystem. This is completely unintuitive, and would probably be frowned upon and discouraged in any programming guide, to call a function "GetXXX", but then have it modify an array you have to initialize and pass into it.

tl;dr: Try this example instead, it will work. Create a new ParticleSystem with default emission behaviour, and attach this script to it. (Note, when using the menu GameObject->CreateOther->ParticleSystem, the ParticleSystem will initially have an x=270 rotation, for whatever reason)

using UnityEngine;
using System.Collections;

public class ParticleSystemTest : MonoBehaviour {

    private ParticleSystem ps;

    void Start () {
        ps=GetComponent<ParticleSystem>();
    }

    void Update () {
        // initialize an array the size of our current particle count
        ParticleSystem.Particle[] particles=new ParticleSystem.Particle[ps.particleCount];

        // *pass* this array to GetParticles...
        int num=ps.GetParticles(particles);
        Debug.Log("Found "+num+" active particles.");
        for(int i=0;i<num;i++){
            if(particles[i].position.z>5) // large local z: let particle drop down
                particles[i].velocity-=Vector3.up*0.1f;
            if(particles[i].position.x>1) // positive x: make it red
                particles[i].color=Color.red;
            if(particles[i].position.x<-1) // negative x: make it die
                particles[i].lifetime=0;
        }
        // re-assign modified array
        ps.SetParticles(particles,num);
    }
}
more ▼

answered Jun 22 '12 at 08:26 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

This appears to do nothing...

var particleTotal = 7;
private var particles = new ParticleSystem.Particle[particleTotal];

function Update ()
{
    particlesLength = particleSystem.GetParticles(particles);

    particles[0].lifespan = 0;
    particles[1].color = Color.red;
    particles[2].position = Vector3.zero;

}
Jun 22 '12 at 08:35 PM Essential

You need to reassign the particle system.

particleSystem.SetParticles(particles,particles.length)

Jun 22 '12 at 08:42 PM Loius

Yeah that's my problem Vicenti. When I do that, the particle system resets.

What I'm trying to achieve is a set of orbs that bounce out that the player can collect. They'll bounce out fine and will slowly disappear over the lifetime rate, and I can detect when a player has collected a particle. I just don't know how to make that individual particle disappear. Using SetParticles the particles all bounce out all over the place again and reset the lifespan.

Jun 23 '12 at 04:36 AM Essential

Well, in the code fragment you posted you are creating an empty array of particles, so there won't be happening much. Usually, this array is initialized by the ParticleSystem. If you create your particles entirely by yourself, generate an empty array as you did, fill it with your desired values (without using GetParticles!), and then apply it with SetParticles, all this in some initialization routine such as Start().

Note that your code fragment shouldn't compile, since there is no property "lifespan", it is called "lifetime". Also note that in your code you set the first particle's lifetime to 0, so the ParticleSystem will automatically remove it before the next Update. This reduces the length of your particles array by one, and at a starting size of 7 you should be almost immediately run into an "array out of bounds" error.

Jun 25 '12 at 03:47 PM Wolfram
(comments are locked)
10|3000 characters needed characters left

So the more I research this, the more I'm beginning to believe that it's impossible to adjust the properties of a settled particle without the whole particle system starting the animation from scratch. If anyone could just confirm this that would be great. Then I can start investigating another option for my bouncing collectable coins...

more ▼

answered Jun 23 '12 at 07:36 PM

Essential gravatar image

Essential
480 48 65 79

see my edited answer

Jun 26 '12 at 10:20 PM Wolfram
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x764
x637
x161
x99
x2

asked: Jun 22 '12 at 08:03 PM

Seen: 1503 times

Last Updated: Jun 26 '12 at 10:23 PM