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. Dismiss Notice

change particle speed "between two constants" via script

Discussion in 'General Graphics' started by san.daniele, Dec 1, 2014.

  1. san.daniele

    san.daniele

    Joined:
    Aug 16, 2013
    Posts:
    19
    The speed of particles in my scene depend on a variable.
    It works like a charm to use
    Code (JavaScript):
    1. particleSystem.startSpeed = LevelSpeed/20;
    But what I actually want is to use the "Start Speed -> Random between two constants" property as found in the particle editor. Can't find out how to change those values through scripting (js).

    Something like:
    particleSystem.startSpeed.ConstantA = LevelSpeed/20;
    particleSystem.startSpeed.ConstantB = LevelSpeed/30;

    I could work around it by creating a second particle system or attach a script to the particle itself … but the above solution would be a lot smoother (at least that's how it appears to me).
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Why not use Random.Range?
     
  3. san.daniele

    san.daniele

    Joined:
    Aug 16, 2013
    Posts:
    19
    do you mean like

    particleSystem.startSpeed = Random.Range(x,y) ?

    unfortunately that just takes ONE random number and applies it to all particles.
    any way I can talk to each emmitted particle?
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    I tested it and changing the startSpeed changes the upper limit while keeping the ratio.

    If your original startSpeed was constant between 1 and 2 and you did:
    Code (csharp):
    1.  
    2. emittor.startSpeed = 10;
    3.  
    the new startSpeeds are now between 5 and 10.

    Hope this helps.
     
    Naphier and san.daniele like this.
  5. san.daniele

    san.daniele

    Joined:
    Aug 16, 2013
    Posts:
    19
    if that helps?
    it actually solves the whole problem! thanks so much!

    (I was actually sure I already tried that)
     
  6. DJT4NN3R

    DJT4NN3R

    Joined:
    Mar 25, 2014
    Posts:
    2
    To anyone who found this post, ParticleSystem.startSpeed is now depreciated. Instead, you'll need to set the startSpeed of the ParticleSystem.MainModule as a MinMaxCurve instead:


    Code (CSharp):
    1. ParticleSystem p = GetComponent<ParticleSystem>();
    2. var v = p.main;
    3. v.startSpeed = new ParticleSystem.MinMaxCurve(5, 10);
     
  7. WorldWideGlide

    WorldWideGlide

    Joined:
    Nov 3, 2013
    Posts:
    26
    Thank you this worked perfectly!
     
    Westland and alexxUID like this.
  8. a_karpins

    a_karpins

    Joined:
    Mar 8, 2023
    Posts:
    8
    This thread helped me find ParticleSystem.MinMaxCurve(), which helped me find my solution, so I thought I'd post my solution here. (I was doing basically the same thing but for startRotation)

    Code (CSharp):
    1. ParticleSystem ps = GetComponent<ParticleSystem>();
    2. ps.Stop();
    3. ParticleSystem.MainModule psm = ps.main;
    4.  
    5. ParticleSystem.MinMaxCurve psmmc = new ParticleSystem.MinMaxCurve(0.0f, Mathf.Deg2Rad * 360.0f);
    6. psmmc.mode = ParticleSystemCurveMode.TwoConstants;
    7.  
    8. psm.startRotation = psmmc;    //This would be startSpeed for you, also your MinMaxValues would be for speed instead of rotation, which doesn't need radians
    9. //Insert whatever else you need to change
    10. ps.Play();
     
    Last edited: Aug 19, 2023