Search Unity

Particle axis constraints and render sorting

Discussion in '2D' started by Hookkshot, Sep 22, 2014.

  1. Hookkshot

    Hookkshot

    Joined:
    Jan 11, 2013
    Posts:
    27
    In the new 2D tools I found it hard to find information on using the particle system, so I wrote this little piece of code to help those who might want to lock the particles to a specific point in the z axis

    Code (CSharp):
    1.  
    2. [AddComponentMenu("EffectsSystem/LockParticles")]
    3. [RequireComponent(typeof(ParticleSystem))]
    4. public class LockParticles : MonoBehaviour {
    5.  
    6.     public float Z = 0;
    7.     public string SortLayerr = "";
    8.     private ParticleSystem ps;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         ps = GetComponent<ParticleSystem>();
    13.         Renderer r = GetComponent<Render>();
    14.         if(r != null && SortLayer != "")
    15.                 r..sortingLayerName = SortLayer;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void LateUpdate () {
    20.         ParticleSystem.Particle[] par = new ParticleSystem.Particle[ps.particleCount];
    21.         ps.GetParticles(par);
    22.         for (int i = 0; i < par.Length; i++ )
    23.         {
    24.             par[i].position = new Vector3(par[i].position.x, par[i].position.y, Z);
    25.         }
    26.         ps.SetParticles(par, ps.particleCount);
    27.     }
    28. }
    I hope this helps someone in the future