Hello,
I’m working on a simply script to play a sound effect when a particle system starts to emit particles. What I currently have works fine for particle systems but does not work for sub-emitted particle systems. I know why it doesn’t work but I don’t know how to get around this issue…
Goal: to play a sound - just once - when a particle system begins emitting particles (including sub emitted particle systems)
So far I have this:
using UnityEngine;
using System.Collections;
public class ParticleSoundEffect : MonoBehaviour
{
public AudioClip SoundEffect;
public float PlaybackVolume = 1;
public float PlaybackSpeed = 1;
private bool effectPlayed = false;
private float timeDelay = 0;
private ParticleSystem parentSystem;
void PlaySound()
{
if(SoundEffect && timeDelay <= 0 && effectPlayed == false)
{
effectPlayed = true;
NGUITools.PlaySound(SoundEffect, PlaybackVolume, PlaybackSpeed);
}
}
void Start()
{
timeDelay = gameObject.particleSystem.startDelay;
parentSystem = transform.parent.GetComponent<ParticleSystem>();
}
void Update()
{
PlaySound();
timeDelay = timeDelay - Time.deltaTime;
}
}
Now the reason this script doesn’t work for sub emitters is that in my case (and I imagine in most people’s) my sub emitter has a delay of 0, but of course does not play after 0 seconds - it plays as soon as the particle system that calls it dies (it is an ‘on death’ type sub emitter).
I could just add the lifetime of the emitted parent particles to the timeDelay variable however this would only work for particle systems that have a static particle lifetime and it still would only play the sound effect once.
The other method is to check if the particle count for a given particle system is greater than 0 (particleSystem.particleCount > 0) however this still only plays the sound once, and only when the first particle dies.
Anyone have an idea how I could make the attached sound effect play as soon as a particle system starts emitting - and every time the particle system starts emitting? Get/SetParticles might have to be part of it…