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

Shuriken: Stopping Emission but NOT destroying existing particles

Discussion in 'Scripting' started by AllanMSmith, Aug 8, 2014.

Thread Status:
Not open for further replies.
  1. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Hey guys,

    The title says it all. I want a particle system to stop emitting particles, but to leave the ones that had been already emitted intact. I already tried particlesystem.enableEmission = false AND particlesystem.Stop(), but they both are yielding the same results...

    What is strange is that I remember that using exactly this code used to work before and particles remained... is there something that changes this behavior, making them all disappear immediately?

    Thanks!
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You need to attach a destroy script to the emmiter to be sure.

    Code (csharp):
    1.  
    2.  
    3. var destroyTime : float;
    4.  
    5. function Start()
    6. {
    7.     Destroy(this.gameObject, destroyTime)
    8. }
    9.  
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Set emissionRate to zero?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Stop() should leave the particles for the remainder of their lifetime. Clear() will remove existing particles immediately. Are you sure your code is correct? If so, it could be a bug.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    If you've tried .enableEmission and it didn't work, I'd suspect there's something wrong with your code or with the way the object is configured. Are you getting any errors in the console?
     
  6. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Yes, im pretty sure.... it is as follows:

    Code (CSharp):
    1. void Awake()
    2. {
    3.      system = GetComponent<ParticleSystem>();
    4.      systems = GetComponentsInChildren<ParticleSystem>();
    5. }
    6.  
    7. void WhenIWantToStop()
    8. {
    9.      for (byte i = 0; i < systems.Length; i++)
    10.      {
    11.           systems[i].enableEmission = false;
    12.      }
    13. }
    14.  
    15. void WhenIWantToStop2()
    16. {
    17.      system.Stop(true);
    18. }
    This is kind of pseudo code, but is exactly what happens in my class... I tried both forms, and the script is always attached to the parent Particle System... in this case to be honest it is only 1 particle system, and as I said, it really used to work, both ways... but this time, it is just hiding everything, I dont know why =/

    @Fluzing: What you are suggesting is the inverse of what I want. I want that the existing particles remain existing for their lifetime values, and not to instantly disappear when I stop the emitter. My class is actually responsible for enabling and disabling the game object for performance purposes, but I checked and it is working correctly as well, it is not disabling the game object at the wrong time and is not what is causing the problem.

    @hpjohn: Reason I dont want to do that is that if Im not mistaken, emissionRate only refers to the Emission/Time and Emission/Distance, but not the burst emissions. Also, I would have to save the number, so I can set it back up when I want to use that system again... and I want to use this class for the whole project, all kinds of particles, which is what I have been doing to be honest.

    Something unexpected is happening now... I play the scene, have the particle system going, then through the inspector I disable the emitter component of shuriken. For exactly 7 frames (going through step by step) the particle system is still visible, and I can see it stopped emitting, while the remaining particles are still alive and behaving normally, but on the 8th frame, all particles abruptly disappear. THEN, the mysterious bit is that if I click on another window (for example on the browser, or Monodevelop... take the focus out of unity) when I click on unity again... the particles that were gone, appear again... but only for one frame... Idk but this is beginning to smell like a bug to me...
     
  7. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    No errors in the console. Even if I manually disable the Emission component of shuriken by hand, through the inspector, the problem happens... also, I've been using this code for a long time and this is the first time its happening...
     
  8. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Ok I think I found out what is causing the problem, BUT it still seems like a bug to me... I was turning everything out and on one thing at a time... and the PARENT of the particle systems (and parent of the code I posted above) had the following coroutine:

    Code (CSharp):
    1.     protected virtual IEnumerator UpdateProjectile()
    2.     {
    3.         while (myGameObject.activeInHierarchy)
    4.         {
    5.             myTransform.rotation = casterHealthHandler.transform.rotation;
    6.             yield return null;
    7.         }
    8.     }
    For some reason, updating the rotation of the parent transform... is making the particles completely disappear.... lol -.-. And the worst is, even though this coroutine was active, the rotation was NOT changing, both objects were not rotating... (not myTransform and casterHealthHandler.transform...)

    Anyway... I will probably reparent the script to casterHealthHandler.transform and then unparent it later on... but this is VERY strange... and yes, THIS is what was causing the problem... if anyone cares to explain why, I would love to know...

    Anyway, thanks for the suggestions everyone!
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Probably just an edge case they didn't account for. Should the particles of an inactive/non-emitting system simulating in local space continue to update with respect to the parent object's transform? The answer probably shouldn't be "no and we're also going to disrespect particle lifetime and turn them all off anyway".
     
  10. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Thing is... its in World space... reparenting and then rotating did work though...
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    world space particle systems shouldn't be affected by transformations to their parent objects.
     
  12. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Yeah I know... only the emitter shape which is what I was looking for... thats why its strange...
     
  13. Deleted User

    Deleted User

    Guest

    I know what you're trying to accomplish. I had the same thing going on. Just check the box Color over Lifetime. Then go and edit the color. At the end of the color line, add a marker, and set the Alpha value to 0. This will make it fade out. I made my script so the particles fade out, then when they're done emitting and fading, delete the obsolete particle system. I hope that helped :)
     
  14. carterdawson

    carterdawson

    Joined:
    Jan 18, 2014
    Posts:
    5
    I had a similar problem to the one described here, but there was no modification to the parent transform. My problem is pretty simple:

    1. Turn on emission
    1.5 Wait, say, 2 seconds to get a good number of particles
    2. Turn off emission
    3. Wait about 4 seconds
    4. All particles disappear at once, early.

    If you turn emission back on within 4 seconds, the particles will go away after their normal lifetime

    As far as I can tell, this is a bug. My workaround was to put code in my Update routine which toggled emission on for 0.1 seconds if it hadn't been turned on recently. My code is here:

    Code (CSharp):
    1.        
    2.  
    3.         //if we have a smoke keepalive
    4.         if (SmokeKeepaliveEverySec > 0.0f)
    5.         {
    6.             //...and the smoke is off
    7.             if (!_is_smoke_on)
    8.             {
    9.                 //and we're overdue for a keepalive...
    10.                 if (Time.fixedTime > _dt_next_keepalive)
    11.                 {
    12.                     float sec_next_keepalive = SmokeKeepaliveEverySec;
    13.                     //then we need to do a keepalive...
    14.                     //if we're currently emitting...
    15.                     if (smokeParticleSystem.enableEmission)
    16.                     {
    17.                         //then turn off emission (remember, we wouldn't be here if smoke were supposed to be on)
    18.                         smokeParticleSystem.enableEmission = false;
    19.                     //otherwise, if we are not currently emitting
    20.                     } else
    21.                     {
    22.                         //set the smoke color to the keepalive color (which is transparent)
    23.                         smokeParticleSystem.startColor = _keepaliveColor;
    24.                         //turn on emission
    25.                         smokeParticleSystem.enableEmission = true;
    26.                         //make a note to turn it off in 0.1 seconds.
    27.                         sec_next_keepalive = 0.1f;
    28.                     }
    29.                     //set our dt_next_keepalive to now plus our keepalive time
    30.                     //make a note to process the keepalive in sec_next_keepalive
    31.                     _dt_next_keepalive = Time.fixedTime + sec_next_keepalive;
    32.                     Debug.Log("Smoke keepalive processed.  smoke on: " + smokeParticleSystem.enableEmission + ", next keepalive in: " + sec_next_keepalive);
    33.                 }
    34.             }
    35.         }

    Hope this helps anyone else who has had this kind of issue.

    Shaun
     
  15. xTheTimeSte

    xTheTimeSte

    Joined:
    Apr 4, 2021
    Posts:
    5
  16. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    MelvMay likes this.
Thread Status:
Not open for further replies.