[Solved] Particle.velocity doesn't affect velocity

Hi,

I’d like to move particles via script, by assigning velocity to each particle, but it doesn’t seem to work. I use standard Particle System (Game Object->Create Other->Particle System), and attach following script:

using UnityEngine;
using System.Collections;

public class ParticleMover: MonoBehaviour {

    void Update()
    {
        ParticleEmitter emitter = (ParticleEmitter)GetComponent("ParticleEmitter");
        for (int i = 0; i < emitter.particleCount; i++)
        {
            Particle p = emitter.particles[i];
            p.velocity = Vector3.right * 2.0f;
            emitter.particles[i] = p;
        }
    }

}

Nothing happens, the emitter works as if no script was attached to its object. I also tried with position with no luck. Is it possible to control each particle behavior via script?

Did you remove/disable the particle animator that automatically gets added to the particle system?

I removed it, but it didn’t change anything.
I’m attaching package with test project.

190247–6740–$particlemover_483.unitypackage (4.54 KB)

I checked the documentation once again, and found out that ParticleEmitter.particles returns a copy of the Particle array.

The code should look like this:

using UnityEngine;
using System.Collections;

public class ParticleMover: MonoBehaviour {

    void Update()
    {
        ParticleEmitter emitter = (ParticleEmitter)GetComponent("ParticleEmitter");
        Particle[] particles = emitter.particles;
        for (int i = 0; i < emitter.particleCount; i++)
        {
            Particle p = particles[i];
            Vector3 direction = Vector3.right;
            float speed = 2.0f;
            p.velocity = direction * speed;
            particles[i] = p;
        }
        emitter.particles = particles;
    }
}

Ok, good to know.