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

Emiting script adjusted particles

Discussion in 'Scripting' started by Arrovs, Mar 13, 2015.

  1. Arrovs

    Arrovs

    Joined:
    Sep 23, 2014
    Posts:
    24
    As we all know particle system have very limited amount of public variables to use, but i came in situation where i need to give my own colors at runtime to particles.
    In my case i needed to give different colors (ideally random between 2 values), but this option isnt accesible.
    So i went to emit them with Emit function. Now i could adjust emit radius around position also.
    But sad part is about randomized sprites. I have 4 sprite bilboard and before it randomized one, but now it uses same one for all - i feel confused how to fix this.
    Does anybody else have experience with custom emitting? I really need all opinions, experiences and ideas as i couldnt found much in internet.
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Perhaps you could try something like:

    Code (CSharp):
    1.  
    2. ParticleSystem system;
    3. Color color; //provide a color between 2 values
    4.  
    5. system.startColor = color;
    6. system.Clear();
    7. system.Simulate(2f, true, false);
    8. system.Play();
    9.  
     
  3. Arrovs

    Arrovs

    Joined:
    Sep 23, 2014
    Posts:
    24
    Heh. I found way what worked well. I added randomSeed to particle struct and that fixed absolutely every problem.
    So now others will know too.

    Code (CSharp):
    1. ParticleSystem.Particle particle = new ParticleSystem.Particle()
    2.                 {
    3.                     position = position,
    4.                     velocity = velocity,
    5.                     size = size,
    6.                     startLifetime = system.startLifetime,
    7.                     lifetime = system.startLifetime,
    8.                     color = colors[Random.Range(0, colors.Length - 1)],
    9.                     rotation = Random.Range(0f, 360f),
    10.                     randomSeed = (uint)Random.Range(1, 10000)
    11.                 };
    12.                 system.Emit(particle);