Spawn a single Particle on each vert of a mesh emitter

I’m trying to emit a single particle on each vert of a mesh emitter. the idea is to use this for a muzzle flash effect.

i’ve run into multiple issues.

  • I can only get the particles to get positioned on one vert…
  • they no longer respect the vert normal for the direction.
  • a particle appears in the origin of the system on the first frame…

Anyone got a better method for doing this?

heres the script…

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


public class ParticleSpawnOnMesh : MonoBehaviour {

        public Mesh myMesh;
        private ParticleSystem                ps                             = null;

    // Use this for initialization
    void Start()
    {
        this.ps = this.GetComponent<ParticleSystem>();
        var main = ps.main;

         var sh = ps.shape;
        sh.enabled = true;
        sh.shapeType = ParticleSystemShapeType.Mesh;
        sh.mesh = myMesh;

        main.maxParticles = myMesh.vertexCount;

    }
    // Update is called once per frame
    void Update ()
    {

         Vector3[] verteces =   myMesh.vertices;
        if ((this.ps != null) && (this.ps.particleCount != 0))
        {
                ParticleSystem.Particle[] particles =  new ParticleSystem.Particle[this.ps.particleCount];
               
                int count = myMesh.vertexCount;// this.ps.GetParticles(particles);
                var emitParams = new ParticleSystem.EmitParams();
               

               
                for (int i = 0, ic = count; i < ic; i++)
                 {

                    emitParams.position = verteces[i];
                    Debug.Log( verteces[i].ToString("F4"));

                }
                 //emitParams.applyShapeToPosition = true;
                 ps.Emit(emitParams, count);
           

               

        }
    }
}

Move the Emit command inside the for loop. So you will emit 1 particle for each vertex.
Else you emit only one particle aster the loop, with the last value set to emitParams.

1 Like

Cheers for the info, i was thinking the emt as a system in ot itself, rather than just a single particle. though the script doesnt emit particles if the emission of the system is 0. I would expect the emit command to still emit particles even when the system is set to zero emission…

grab the mesh.normals and set the particle velocity to that vector * startSpeed, to get the correct direction.

FYI we are currently adding stuff to make this possible without any scripts. We recently discovered that the Legacy Particle System supports something similar (Systematic mode) and we need to replicate its functionality in the current Particle System as we are about to remove Legacy Particles!

awesome, I was going to ask about getting in as native functionality…

as fun as its been to learn about the Emit command, it has taken the day to almost get it working: /