Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Particle emitter almost doing what I want with C# but not quiet there yet. Help needed.

Discussion in 'Scripting' started by GFFG, Nov 26, 2014.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi,

    Im trying to make a little game where you can fly a spaceship with keys (W,S,A,D) so far the ship is flying nicely and I have a particle emitter that reacts when I press and hold these keys. But the particles don't disappear when I release the key it sort of freezes and remains still. Heres the code I have so far only using the key "W":
    Code (CSharp):
    1. public class ParticleEmitter1: MonoBehaviour
    2. {
    3.     public ParticleEmitter smallflames2;
    4.  
    5.  
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13.             void Update () {
    14.  
    15.     if(Input.GetKey(KeyCode.W)){
    16.             smallflames2.particleEmitter.enabled = true;
    17.     }else if(Input.GetKeyUp(KeyCode.W)){
    18.             smallflames2.particleEmitter.enabled = false;;
    19.        
    20.        
    21.     }
    22. }
    23. }
    I would really appreciate any help I can get on this issue, it's so close to working perfectly. Any ideas?
     
  2. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    I posted several times without getting any answers from the community, Daddy's not mad, he's disappointed! :)


    I've been stuck with this silly little issue for 14 days now and its driving me insane!



    I have a prefab of a particle emitter that looks like thrusters of an engine, and its activated with the key "w".

    My issue is that the particle emitter starts and stops as it should but the particles don't disappear when I release the key. heres the code I have so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ParticleEmitter1: MonoBehaviour
    5. {
    6.     public ParticleEmitter smallflames2;
    7.  
    8.  
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16.             void Update () {
    17.  
    18.     if(Input.GetKey(KeyCode.W)){
    19.             smallflames2.particleEmitter.enabled = true;
    20.  
    21.     }else if(Input.GetKeyUp(KeyCode.W)){
    22.             smallflames2.particleEmitter.enabled = false;
    23.  
    24.  
    25.  
    26.        
    27.        
    28.     }
    29. }
    30. }

    Something's missing at the end that tells the "smallflames2" to be invisible upon release of the key. PLEASE HELP!!!!!!!!! before I loose my mind.
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Replace else if with else. Remove all contents from brackets.
     
  4. 3agle

    3agle

    Joined:
    Jul 9, 2012
    Posts:
    508
    Use GetKeyDown instead of GetKey
     
  5. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    GFFG likes this.
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Why not just make your particles world space so they won't follow you and will realistically disperse? You can have them change color, grow, shrink, become transparent, slow down, speed up, etc.
     
    elmar1028 likes this.
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Daddy needs verbose topic titles.
     
    djfunkey, GFFG, der_r and 1 other person like this.
  8. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Daddy also needs a xanax
     
    GFFG and elmar1028 like this.
  9. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,660
    What you're doing at the moment is enabling/disabling the particle emitter, which means that when the key is released it will stop creating new particles. This has no impact on any particles that already existed.

    If you want to delete all the particles that already existed, use particleEmitter.ClearParticles().
     
    GFFG likes this.
  10. Aurore

    Aurore

    Director of Real-Time Learning

    Joined:
    Aug 1, 2012
    Posts:
    3,106
    Threads have been merged.
     
    Tomnnn and elmar1028 like this.
  11. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Never knew it was possible! :O

    This is like a new way to combat duplicate threads.
     
    Tomnnn likes this.
  12. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    I didn't know about that either. I'm impressed and afraid.
     
  13. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    You guys are great! thanks, Now Daddy's calm and xanaxed ;-) I will try everything suggested and truly appreciate your help. Will update if/when it works
     
  14. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Here we go. I had a friend of mine teach me some things that REALLY helped in regards to this particle issue.
    Basically I had the right idea but I just had to enable/disable the particle Renderer component in accordance with holding down the key. Here's the code, hope it helps others in the same situation:
    Code (CSharp):
    1. public class ParticleEmitter1: MonoBehaviour
    2. {
    3.     public ParticleEmitter smallflames2;
    4.  
    5.     private ParticleRenderer thisParticleRenderer;
    6.  
    7.  
    8.     void Start () {
    9.         thisParticleRenderer = gameObject.GetComponent<ParticleRenderer> ();
    10.         smallflames2.particleEmitter.enabled = true;
    11.     }
    12.  
    13.  
    14.  
    15.  
    16.  
    17.     void Update () {
    18.         if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.D))
    19.         {
    20.  
    21.             thisParticleRenderer.enabled = true;
    22.        
    23.  
    24.         }
    25.         else
    26.         {
    27.             thisParticleRenderer.enabled = false;
    28.         }
    29.  
    30.     }
    31.    
    32. }
    33.