- Home /
How do I make a particle emitter emit when a key is pressed?
I feel like this code is really close to doing what I need to do. I have an empty game object with all the things needed for a particle effect that is the child of another mesh. What I want it to do is to make the emitter emit for a time when I press the 1 key on the numpad, and then have the emitter turn off. This isn't as fluid as I want it, however it's the best I can do for now.
The Code:
var emissionTime : float = 3.0;
var emissionDelay : float = 3.0;
function Start() { TimedEmit();
}
function TimedEmit() {
GetComponent(ParticleAnimator).autodestruct = false;
if (Input.GetKeyDown ("[1]"))
particleEmitter.emit = true;
yield WaitForSeconds(emissionTime);
particleEmitter.emit = false;
yield WaitForSeconds(emissionDelay);
}
Answer by DaveA · Jul 16, 2011 at 12:57 AM
I would use an Update function, but in any case, use Input.GetKeyDown(KeyCode.Keypad1); Do you need the final WaitForSeconds? After setting emit to false, it will trail off as particles decay themselves.
Actually, this would only work as-is if you had the Num 1 key down when the game started (or this object comes into being). Is that what you meant?
var emissionTime : float = 3.0;
var emissionDelay : float = 3.0;
var lastTime = 0.0;
function Update()
{
if (Input.GetKeyDown (KeyCode.Keypad1))
{
GetComponent(ParticleAnimator).autodestruct = false;
particleEmitter.emit = true;
lastTime = Time.time;
}
if (particleEmitter.emit && (Time.time - lastTime) > emissionTime)
{
particleEmitter.emit = false;
}
}
I like the way that it trails off but I'm trying to make it so that whenever the button is pressed the particles emit. For example if this were for a flamethrower and I ran toward enemies and used it, then have the wait, then do it again when the button is pressed again. Any ideas?