Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity ParticleSystem Play and Stop

Tags:

c#

unity3d

unity5

My particlesystem.Play() does not work. I have spent few hours on this issue and still could not figure out.

Whenever my character hits an object, it will call the function particleAuraPlay(), and the log message "HIT" is showing which means the function is being called properly.

When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing. Can anyone please solve my problem?

using UnityEngine;
using System.Collections;

public class ParticleController : MonoBehaviour {

    private bool playAura = false;
    private ParticleSystem particleObject;

    void Start () {
        particleObject = GetComponent<ParticleSystem>();
        particleObject.Stop();
    }


    void Update () {
        if (playAura)
        {
            Debug.Log("Running");
            particleObject.Play();
        }

    }

    public void particleAuraPlay()
    {
        Debug.Log("HIT");
        playAura = true;
    }
}
like image 913
bubibu Avatar asked Oct 07 '16 12:10

bubibu


2 Answers

When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing

The is a logic error.

When the particleAuraPlay() function is called, playAura is set to true.

In the Update functions,particleObject.Play(); will be called every frame since playAura is true.

You can't do this. Calling particleObject.Play(); every frame will not actually do anything as it will attempt to play and stop particles in each frame resulting to no particles at-all.

The solution is to check if playAura is true, if true, call the particleObject.Play(); function then set playAura to false so that the particleObject.Play(); function won't be called again until particleAuraPlay() is called again.

The new Update() function fixes the logic error:

void Update()
{
    if (playAura)
    {
        Debug.Log("Running");
        particleObject.Play();
        playAura = false;
    }
}
like image 144
Programmer Avatar answered Sep 18 '22 01:09

Programmer


In your particleAuraPlay() function you just need to check if your ParicleSystem is running or not you can achieve this by this code...

 public void particleAuraPlay(){
      if(!particleObject.isPlaying){
          particleObject.Play();
       }     
    }
like image 45
Vivek Avatar answered Sep 20 '22 01:09

Vivek