Search Unity

Setting the burst interval of a particle system?

Discussion in 'Scripting' started by gevarre, Sep 26, 2018.

  1. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Anyone know how to set the interval of a burst in the Emission section of a particle system? The manual doesn't go into any details on how to do this, but the closest I can get from what they do say is something like this:

    Code (CSharp):
    1. public ParticleSystem smokePuff;
    2. smokePuff.Burst.repeatInterval = 1.0f;
    But of course that doesn't work. I get this error:
    `Burst': cannot reference a type through an expression. Consider using `UnityEngine.ParticleSystem.Burst' instead

    Any ideas? Thanks.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Thanks, but the first one doesn't refer to interval at all, and neither give any examples of actual usage, so no matter what I try, I get errors.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  5. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Sorry, but that's still not doing it. Looking up "ParticleSystem.emissionModule.Get/SetBursts" doesn't show anything at all (as far as I can see) about setting the interval, only the burst size. This would be a lot easier with an actual line of code, but thanks for trying to help.
     
  6. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Sorry for the confusion, but it looks like I need to be more specific about what I'm asking for, so I should start over:

    Issue:
    I have a vehicle. It has a smokestack with smoke coming out in puffs, like a train. As it's speed increases, I need to change the Interval float of the existing Burst in the Emission section of the particle system so that the puff bursts speed up. Since this is a gradual change and is timed to other things happening (like sounds), I can't stop it, change the value, and start it again, and I can't just create a new burst every time I update.

    I do a similar thing with accompanying sounds like this:
    Code (CSharp):
    1. audio_SmokePuff.pitch = puffPitch;
    It very easy to access the variables in things like audioSources, but unfortunately, the new particle system was not written to allow for what has become standard pseudo-c# Unity scripting practices (there are complaints about this in numerous other forums). I'm know they had their reasons, but it makes scripting rather difficult, which brings up my situation:

    I, like a good percentage of people who post here, am a scripter, and very definitely am not a programmer. I consider myself very good at looking at a snippet of Unity code/script and being able to deconstruct and adapt it for my needs, but if someone refers to an abstract programming practice such as "Use Get/Set Bursts on the Emission module" without further explanation, I have no example to deconstruct and I don't have the programming background to apply the appropriate C# rules to figure out how to make it work. I need some kind of example, and the Unity documentation is very inconsistent in providing this, and when they do, they generally don't go far enough.

    Thank you for the previous links. Those were the first pages I visited when I began my search on how to do this. I do actually do as much research as possible before posting here because I don't want to waste people's time. Unfortunately, all of the Unity examples only show how to create a new burst, not modify an existing one, and the parameters set by these examples only include the time the burst happens, and the number of particles created. And even if I assume that I can just add additional parameters for "Cycles" and "Interval", it's not clear how to write this, like, how do I set the Cycles to Infinite? I very well may be missing something obvious, and if so I apologize, but everything I've tried so far just ends with errors like "`Burst': cannot reference a type through an expression. Consider using `UnityEngine.ParticleSystem.Burst' instead". That's obviously a reference to a particular coding convention that I'm missing, but I don't know how to correct it.

    So...
    Does anyone have an example they can share of how to change the Interval setting of an existing burst in the emission module? I don't necessarily even need full code. Pseudo code would be just fine as long as it shows all the steps necessary. I'm starting to believe it's not actually possible because of the way the system is coded and integrated into the engine, but I'm hoping someone has a solution. My next step is official Unity Support, but I'm not holding my breath on that one.

    And thank you if you've read all this and the attempts made so far to help me. I really do appreciate it :)
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  8. gevarre

    gevarre

    Joined:
    Jan 19, 2009
    Posts:
    132
    Aha. Finally got it:
    Code (CSharp):
    1. var emission = smokePuff.emission;
    2. ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emission.burstCount];
    3. emission.GetBursts(bursts);          
    4. bursts[0].repeatInterval = puffSmokeCurve.Evaluate(speedNormal);
    5. emission.SetBursts(bursts);
    There were several little things, but the big thing was that I was forgetting that bursts is actually an array. Of course now that I look at it, it's obvious.

    Thanks for your patience :)
     
    unity_8C7oN2IUXThZ6A likes this.
  9. jardle1

    jardle1

    Joined:
    Jun 4, 2021
    Posts:
    2
    What's going on here is that Particle Systems don't let you edit a burst directly - rather, you need to create a ParticleSystem.Burst[] and use myParticleSystem.emission.SetBursts. Alternatively, if you're only editing one burst, you can use SetBurst with whatever index you desire to edit.

    So you could clean things up like this:
    Code (CSharp):
    1. ParticleSystem.Burst burst = myParticleSystem.emission.GetBurst(0);
    2. burst.repeatInterval = myFloat;
    3. myParticleSystem.emission.SetBurst(0, burst);