Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Activate smoke on input and on a timer?

Discussion in 'Scripting' started by Tyler Ridings, Mar 24, 2011.

  1. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Ok guys here is my question.I have a particle emitter witch makes it look like smoke is coming out of my smoke stacks on my truck.What i want to do is to active the particle emitter on key input with this script

    Code (csharp):
    1. function Update() {
    2.    if (Input.GetKey("verticle")) {
    3.       hingeJoint.useMotor = true;
    4.    } else {
    5.       hingeJoint.useMotor = false;
    6.    }
    7.  
    That script is for a hingeJoint so what do i need to replace "hingeJoint" with in the script so it will apply to my particle emitter.

    My second question is how can i put it on a timer so say my key input is down for 30 seconds the particle emitter only pushes smoke out for 10 seconds?
     
  2. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    I don't think you can call "Input.GetKey" with vertical (thought I could be wrong). That being said, you could do the same kind of script with "Input.GetAxis". Here is a quick example:

    Code (csharp):
    1. var timer : float;
    2. var vertInput : float;
    3. var absVertInput : float;
    4. var smokeOn : boolean;
    5. var moving : boolean;
    6.  
    7. function Start() {
    8.     smokeOn = false;
    9.     moving = false;
    10.     particleEmitter.emit = false;
    11.  
    12. }
    13. function Update () {
    14.     vertInput = Input.GetAxis("Vertical");
    15.     absVertInput = Mathf.Abs(vertInput);
    16.     if (absVertInput > 0)
    17.     {
    18.         if (!moving)
    19.         {
    20.             moving = true;
    21.             smokeOn = true;
    22.             timer = Time.time;
    23.             particleEmitter.emit = true;
    24.         }
    25.         if (smokeOn  Time.time - timer > 10)
    26.         {
    27.             smokeOn = false;
    28.             particleEmitter.emit = false;
    29.         }
    30.     }
    31.     else
    32.     {
    33.         moving = false;
    34.         smokeOn = false;
    35.         particleEmitter.emit = false;
    36.     }
    37. }
    Just add this script to a GameObject with a particleEmitter attached to it to try it out.
     
  3. Tyler Ridings

    Tyler Ridings

    Joined:
    Aug 28, 2009
    Posts:
    201
    Thats exactly what i needed.Thanks so much because not only did i get what i needed but i have learnt a few things.Like i new i had to call for "particleEmitter" but didnt know i needed to put .emit after.Also ive learnt how to do a timer and that you can only do axis with verticle.Thanks!