Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Shuriken particle system - accumulative emission on key down (C#)

Discussion in 'Scripting' started by GFFG, Jul 31, 2016.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi, How can I make a shuriken particle system emit more and more the longer I press a key until it reaches, lets say 90 particles per second, and then stay at 90 until I press a different key to make it emit less and less until it reaches 0?

    I would also like for the start speed to be affected in accordance with the emission amount so that when the emission rate reaches maximum of 90 particles per second the start speed also reaches its maximum of 32 for example.

    I found this thread but it only turns emission on or off on key press : http://answers.unity3d.com/questions/423398/particle-burst-on-button-press.html
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    You can set the rate value via script: http://docs.unity3d.com/ScriptReference/ParticleSystem.EmissionModule-rate.html
    So write a script that lerps from a start emit value to a max emit value(90) over x seconds.

    Something along these lines:

    Code (CSharp):
    1. public float startTime;
    2. public float duration = 10;
    3. // 10 secs
    4. ...
    5. on button down
    6.     startTime = Time.time;
    7. ....
    8. rate = Mathf.lerp(0, 90, (Time.time - startTime) / duration);
    9. ...
     
    GFFG likes this.
  3. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi Karl,

    Thanks for the explanation. I've tried the following :
    Code (CSharp):
    1.     public ParticleSystem.MinMaxCurve rate;
    2.  
    3.     public float startTime;
    4.     public float duration = 10;
    5.  
    6.     void Update(){
    7.  
    8.         if (Input.GetKey (KeyCode.Z)) {
    9.             startTime = Time.time;
    10.             rate = Mathf.Lerp(0, 90, (Time.time - startTime) / duration);
    11.            
    12.        
    13.  
    14.         }
    But I get the Error cannot convert float to Min.MaxCurve:
    "error CS0029: Cannot implicitly convert type `float' to `UnityEngine.ParticleSystem.MinMaxCurve'"

    Is there another way to write the script that works without this float conversion error?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    Just do
    Code (CSharp):
    1.  
    2. var rateModule = particleSystem.emission;
    3. rateModule.rate = new MinMaxCurve( Mathf.Lerp(0, 90, (Time.time - startTime) / duration));
    4.  
     
    Last edited: Aug 6, 2016
    GFFG likes this.
  5. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Can I trouble you for a C# version ? It's incorporated into a much larger C# script so I can't use Javascript
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    t
    that is c# :)
     
    GFFG likes this.
  7. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Wow I'm sorry and embarrassed o_O Thank you very much for your help :)
     
    karl_jones likes this.
  8. MaximumTre

    MaximumTre

    Joined:
    Nov 28, 2015
    Posts:
    163
    Maybe this isn't the place to ask this, but I also noticed that the particle system has changed and had a question about how to change the rate particles are spawned, so now some of my code is deprecated. Why do all of these extra steps? Was there some reason ParticleSystem.emissionRate was changed or did this just add new functionality I'm not aware of?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,850
    It was changed to fully expose the particle system via script.
    If you just want to change the rate do

    Code (CSharp):
    1. var emissionModule = myParticleSystem.emission;
    2. emissionModule.rate = 123;
     
    MaximumTre likes this.