Search Unity

Particle emitters...

Discussion in 'Editor & General Support' started by Richard_B, Aug 8, 2005.

  1. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    I want to stop a particle emitter emitting just after it has fired one shot. At the moment I am it doing via

    Code (csharp):
    1.  
    2. var lifeTime = 3.0;
    3. var startTime = 0.0;
    4.  
    5. function Awake(){
    6.     startTime = Time.time;
    7.     }
    8. function Update () {
    9.    
    10.     if (Time.time - startTime > lifeTime) {
    11.         gameObject.particleEmitter.emit = false;
    12.     }
    13. }
    but I have to hand tweak the lifeTime to stop the emmision between "shots" - is there a better way to stop emission after one shot?

    thanks,
    Richard.

    PS The emitter is being spawned at some unknown time.
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you enable 'oneshot', all particles will spawn at the same time, and no other particles will be spawned before some of them die.

    This should give you plenty of room.

    Do something like:
    Code (csharp):
    1.  
    2. var lifeTime = 3.0;
    3. var startTime = 0.0;
    4.  
    5. function Awake(){
    6.    startTime = Time.time;
    7.    // Call the stopme function halfway before the minimum
    8.    //energy of the particles are spent. This is a good margin.
    9.    Invoke ("StopMe", particleEmitter.minEnergy * .5F);
    10.    }
    11.  
    12. function StopMe () {
    13.   particleEmitter.emit = false;
    14. }
    15.  
    A few points:
    * Instead of checking every frame if you've spent too much time, it is a bit faster to just use Invoke to get a call a bit later in time
    * You don't need gameObject.particleEmitter - particleEmitter will do - saves a bit of typing.

    Hope this helps.
     
  3. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Excellent - just the type of tips I need - many thanks.

    On a similar note, I want to re-activate the game object a random time after it has been de-activated (by a user mouseDown). So, at the momnet I was thinking to again check every frame to see if the random time interval has passed - is there a better way of doing this sort of thing.

    thanks again,
    Richard.
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Actually, when you deactivate the GO, the script no longer gets update calls.

    I _think_ you can still use Instantiate, though....

    sth. like:
    Code (csharp):
    1.  
    2. function OnMouseDown () {
    3.    Invoke ("WakeMe", 3); // Wake me up again in 3 seconds
    4.    gameObject.active = false;
    5. }
    6.  
    7. function WakeMe () {
    8.   gameObject.active = true;
    9. }
    10.  
     
  5. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    I checked through the docs after your previous suggestion and tried the coroutine thing:

    Code (csharp):
    1.  
    2. var mushRoomPuffParticles : GameObject;          
    3.  
    4.  
    5. function Update () {  
    6. }    
    7.  
    8. function OnMouseDown(){
    9.     if (Input.GetMouseButtonDown (0) ) {  
    10.       var  mr : GameObject = Instantiate (mushRoomPuffParticles, transform.position, Quaternion.FromToRotation (Vector3.fwd, transform.position));
    11.     }  
    12.      mr.particleEmitter.emit = true;
    13.      gameObject.active = false;  
    14.      var rndTime =  Random.Range(2.0,4.0);
    15.      StartCoroutine("reactivateMushroom",rndTime);
    16. }
    17.  
    18. function reactivateMushroom(time){
    19.    
    20.     yield new WaitForSeconds (time);
    21.     gameObject.active = true;
    22.    
    23. }  
    Seems to work beautifully!

    thanks again,
    Richard.
     
  6. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Mmmm... not quite so well - I have to destroy all the emitters that are Instantiated!

    R.
     
  7. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Simple - just make 'mr' a local variable, so you can get to it later.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You could use the particle animators autoDestruct variable.
    It will destroy the particle system once it runs out of particles. Seems like it is exactly what you want.
     
  9. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Hi Joachim,
    Eerr it doesn't seem to work. I have "one shot" and "autodestruct" switched on. In the Game view I turn on Play, but the emitter just keeps shooting away.

    Richard.

    thanks,
    Richard.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    autoDestruct destroys the particle emitter once it has run out of particles.
    If you don't stop emission of new particles it will not destroy itself.
     
  11. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Sorry - I must be missing something, but how do I stop the emission of particles? If I switch off emit (ie = false) then all the particles disappear. If I set the min and max energy to zero, then emission stops - is that what I am suposed to do?

    many thanks,
    Richard.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    emission should be stopped like this:
    Code (csharp):
    1.  
    2. particleEmitter.emit = false;
    3.  
    The expected behaviour is that it stops emitting particles from that frame on. It does not immediately delete already spawned particles.

    If this is not what you see, please file a bug, with script and sample scene.
     
  13. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    What you say is what happens - I am not sure what I was doing wrong previously. Thanks very much for your help - Richard.