Play sound on particle emit (sub emitter)

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…

I expanded the above answer into a script that can play a sound on particle birth as well. You may need to change the SoundManager.Play line into whatever you use.

@script RequireComponent(AudioSource)

public var OnBirthSound : AudioClip;
public var OnDeathSound : AudioClip;

private var _numberOfParticles : int = 0;

function Update(){ 
	if (!OnBirthSound && !OnDeathSound){ return; }
	var count = particleSystem.particleCount;
	if (count < _numberOfParticles){ //particle has died
		SoundManager.Play(audio, OnDeathSound); 
	}else if (count > _numberOfParticles){ //particle has been born
		SoundManager.Play(audio, OnBirthSound); 
	}
	_numberOfParticles = count; 
}

My solution to this is to check the particle count in the system on update…
if the particles are less then previous particles… hence the particle has just died:

private void Update() { if (particleSystem.particleCount < _numberOfParticles) { //play Sound} _numberOfParticles = particleSystem.particleCount;
}`

I go through my solution here - even though it’s 5 years since the question was asked :slight_smile:

Enjoy!

If the particle system emits new particles slowly, you can check the particle count. If the count is less than previous count, it means some particles died.

However, you can not get the correct position of dead particles. If the system is always emitting new particles, the particle count may be always increasing and the solution cannot work.

If you need to know the particle position, you can try this:

    	private void LateUpdate()
    	{
    		var count = _particleSystem.GetParticles(_particles);
    		for (var i = 0; i < count; i++)
    		{
    			if (_particles.remainingLifetime >= 0 && _particles*.remainingLifetime <= (isUnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime))*
 _*			{*_
 _*				// Do something you want here*_
 _*			}*_
 _*		}*_
 _*	}*_
_*
*_

This solusion may cause the sound to play 1 frame ahead, but you can use coroutine to delay 1 frame to play the sound. If your frame rate is stable, it will work perfectly.