- Home /
How to play a ParticleSystem just once when a certain value is reached
Hi, I want to play the Particle system (with children ) just once, when the progress bar reaches 100%. When I use Play method under the Update function, it works, but it loops (I know this is not the right way) . Since I am new to C#, please help me with the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleController : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
// With this statement below the Particle System bursts at the end, but it loops even though Looping is unchecked in the editor
else {
particleBurst.Play();
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
/*
// With this statement below nothing happens.
/* public void ParticleBurst () {
if (currentAmount == 100) {
particleBurst.Play ();
// particleBurst.Emit (30);
}
}
*/
}
Answer by KKS21199 · Apr 06, 2017 at 05:25 PM
Hey,
you can use particleBurst.Emit();
instead of particleBurst.play();
and it should only play once.
You can also use Play by unchecking the Looping option in the inspector and also set the duration to the number of seconds you want the particle to play. (by default it is 5)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleController : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
// With this statement below the Particle System bursts at the end, but it loops even though Looping is unchecked in the editor
else {
particleBurst.Emit(100);
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}
thanks a lot! unchecking the Looping option doesn't work for me. I use emit();
Answer by lostframe77 · Apr 07, 2017 at 11:20 PM
@Sugavanas thanks for the answer, but I have tried this before and it didn't work. In the meantime I have found the solution. This code works in my case...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleBurst : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
private bool played;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
else
if (!played) {
particleBurst.Play ();
played = true;
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}
Your answer
Follow this Question
Related Questions
Animating key on AnimationCurve in real time 0 Answers
Using a Burst in a script 3 Answers
Local Party Game Score Transfer 1 Answer
How to get float value from other script? 2 Answers
(SOLVED) [System.Serializable] / Inspector is not working 2 Answers