- Home /
Activate a particle system on click c# script
Hi,
I am actually new to Unity and i have a question.
I actually have a first person controller and a weapon attached to it. I put a particle system on my scene (https://www.assetstore.unity3d.com/en/#!/content/1) and i would like to activate it when I press 'fire'
This is my script (on my first person controller)
public Transform origin;
public Object bullet;
public ParticleSystem detonator;
private float lastShoot = 0;
// Update is called once per frame
void Update ()
{
// pas de spamm de clic
if (Input.GetButton ("Fire1") && Time.realtimeSinceStartup - lastShoot > (1.0/4.0)) {
lastShoot = Time.realtimeSinceStartup;
// animation
detonator.enableEmission = true;
detonator.Play();
}
}
Nothing appens, my particle system is not playing.
If you have any ideas.. Have a good weekend
I bet it's better to use 1.0f/4.0f
if you did mentioned floats, not doubles. )
Answer by aliaavatar · Dec 22, 2014 at 10:37 AM
Put you ParticleSytem Prefab in you Resource folder. Then Load it into the Scene. Then use Instantiate(). I'm using the Elementals Particle System myself: https://www.assetstore.unity3d.com/en/#!/content/11158
Your case is simpler, since you only need to Instantiate the Particle System on the Weapon's tip and parent it to the weapon: use particle.transform.parent = weapon.transform;
Here's my code. It gets a bit more complicates here, as I make the splash as the cannons hit the water, but it's useful if you want to add explosions to wherever the bullets hit.
public Object Splasher;
void Start()
{
Splasher = Resources.Load("Splash"); }
public void Make_a_Splash(GameObject canonball) {
GameObject particle = GameObject.Instantiate(Splasher, canonball.transform.position, Quaternion.identity) as GameObject;
StartCoroutine(Cleanup_a_Blast(particle));
}
void Update() {
if(Input.GetKeyUp(KeyCode.Alpha1))
...Make_a_Splash(GameObject canonball)
//adds the splash to where the cannons hit. //Cannons need to have a collider as a trigger attached.
}
public IEnumerator Cleanup_a_Blast(GameObject blast)
{
yield return new WaitForSeconds(2);
GameObject.Destroy(blast);
}