Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Particle System, placing EXISTING particles

Discussion in 'Scripting' started by naked_chicken, Nov 27, 2012.

  1. naked_chicken

    naked_chicken

    Joined:
    Sep 10, 2012
    Posts:
    186
    I've scoured the web and found many examples of how to create new particles within a particle system and set their position, color and all that but I can't seem to find anything on how to affect existing particles. GetParticles just returns the number of particles within an array of new particles you create.

    How does one take all the particles that have already been emitted and, say, change their position?
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    You need to get the Particles from the Emitter. You can access the particle property to get an array of Particle[]

    Documentation here

    Once you have the array, iterate over the collection and modify.
     
  3. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    1. GetParticles.
    2. Change their positions.
    3. SetParticles.
     
  4. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    1. GetParticles...Pass in array, returns an int, the amount, from the documentation -> " Returns the number of particles written to the input particle array."
    2. Nope
    3. Yes, after you get the array of Particle type from the emitter, it specifically tell you that you must set after modifying, so yeah.
     
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    What do you mean?

    Code (csharp):
    1.     private ParticleSystem.Particle[] buffer = new ParticleSystem.Particle[100];
    2.  
    3.     private void Update()
    4.     {
    5.         // Resize the buffer in case it is too small.
    6.         var particleCount = particleSystem.particleCount;
    7.         if (buffer.Length < particleCount)
    8.         {
    9.             buffer = new ParticleSystem.Particle[particleCount];
    10.         }
    11.  
    12.         // Update particle positions.
    13.         particleSystem.GetParticles(buffer);
    14.         for (int i = 0; i < particleCount; i++)
    15.         {
    16.             buffer[i].position = 100 * Random.insideUnitSphere;
    17.         }
    18.         particleSystem.SetParticles(buffer, particleCount);
    19.     }
    20.  
     
  6. naked_chicken

    naked_chicken

    Joined:
    Sep 10, 2012
    Posts:
    186
    Landern - that's for the legacy particle emitters, I'm talking about the shuriken particle system.

    Alexzzzz - Get particles doesn't actually get the particles as Landern said, it just returns the amount which I don't get at all, couldn't you just use particleCount. And as I said in my post I know how to create a new ParticleSystem.Particle array like in your example. The trouble I'm having is getting to the particles that already exist. It's completely doable with the legacy particle emitters so I don't know why they decided we shouldn't be able to do it with shrunken.
     
  7. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    My bad, when i was looking up the documentation i thought i was being very aware of the legacy particle system.

    I do believe Alexzzzz is correct, particle is a struct, the array passed to GetParticles(size depending) will fill the array up and return the count as well, after manipulation, you have to update the particles invoking SetParticles since the array you filled is just a copy of what was emitted.
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Correct.