- Home /
Particle Burst on Button Press
Hi. I just got over looking a particle systems in Unity and I need help figuring something out.
I'm currently using the Shuriken particle system in Unity 4. I'd like to sync a button press to trigger not only an increase of speed in my particle system, but trigger the particle burst feature. I've looked through the documentation on Unity's website but it's rather incomplete when it comes to Particle Systems. I've tried to code a few things and it's become rather hit or miss as to whether Unity will accept the code for things, though I will admit I'm not the best programmer.
I've been taking a few small steps trying to learn how to program the particle system. So far, the most progress I've had is:
using UnityEngine;
using System.Collections;
public class particleOnOff : MonoBehaviour {
// Use this for initialization
void Start () {
gameObject.particleSystem.enableEmission = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.JoystickButton0)) {
gameObject.particleSystem.enableEmission = true;
}
/*if (gameObject.particleSystem.enableEmission = true && Input.GetKeyDown(KeyCode.JoystickButton0)) {
gameObject.particleSystem.enableEmission = false;
}*/
}
}
Now I'm using a unique gamepad for this project, the Nintendo DK Bongos, which is why it's reading joystick buttons, though I've also tested this with regular buttons. It works, but it only turns the particles on only. The commented out part is because I wanted to make the bongos turn emission on and off. Problem is that adding that commented part into the code makes it so the particle system won't work at all. Adding a normal if or else statement after the first if statement doesn't seem to work either. I consider this the first step to understanding the particle system, but I'm getting frustrated at getting these bursts to work. Can anyone help me?
Answer by Chronos-L · Mar 24, 2013 at 01:42 AM
Have you took a look at ParticleSystem.Emit( count:int )?
If you need a quick burst of 10 particles when you click
public class ParticleController : MonoBehaviour {
void Start () {
// You can use particleSystem instead of
// gameObject.particleSystem.
// They are the same, if I may say so
particleSystem.emissionRate = 0;
}
void Update () {
if( Input.GetKeyDown( KeyCode.A ) ) {
particleSystem.Emit(10);
}
}
}
if you need X number of particles per second while touching:
if (Input.GetKey(KeyCode.A)) {
particleSystem.Emit( (int) (particlePerSecond * Time.deltaTime) );
}
I have, but when I was fooling around with that parameter, it just refused to work and I moved on to the enableEmission function. If you don't $$anonymous$$d. Could you post an example?
enableEmission
is more suitable for something like turn on the fire, start falling leaves etc
You are looking for burst, so you should use emissionRate=0
with Emit(count)
.
I edited my answer, take a look at it. I use the A key to burst particles.
Holy crap! It works awesomely! Thank you so much! You've saved me so much time and frustration! :D
One thing though:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
particleSystem.Emit( (int) (particlePerSecond * Time.deltaTime) );
}
I tried editing the int and particlePerSecond parameter with numbers, but I get a series of errors. How exactly particlePerSecond parameter work?
It is a parameter from you script.
public int particlePerSecond = 75;
...
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
particleSystem.Emit( (int) (particlePerSecond * Time.deltaTime) );
}
By using particlePerSecond * Time.deltaTime
, you will get a rough estimation on how many particles should be emitted on that particular Update()
so it will fit particlePerSecond
.
What errors are you getting?
Oh, I didn't have a particlePerSecond variable. I added it, but I'm still getting errors.
Assets/Standard Assets/Scripts/particleOnOff.cs(17,30): error CS0119: Expression denotes a value', where a
method group' was expected
Assets/Standard Assets/Scripts/particleOnOff.cs(17,20): error CS1502: The best overloaded method match for UnityEngine.ParticleSystem.Emit(int)' has some invalid arguments Assets/Standard Assets/Scripts/particleOnOff.cs(17,20): error CS1503: Argument
#1' cannot convert object' expression to type
int'
Your answer
Follow this Question
Related Questions
Scale Shuriken Particle System with Parent Transform 5 Answers
Particles emmiting all over the shape, is it possible make them spawn gradually from a set point? 0 Answers
PS Custom Vertex Streams to Particle Trail 0 Answers
Setting the position of particles in a (shuriken) ParticleSystem 3 Answers
Multiple trails behind one particle? 1 Answer