Skip to content

Instantly share code, notes, and snippets.

@cuongnmx
Forked from Laumania/ParticleSoundSystem
Created November 10, 2020 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongnmx/0fe0eddcf80790f5096c1acc1503d8dd to your computer and use it in GitHub Desktop.
Save cuongnmx/0fe0eddcf80790f5096c1acc1503d8dd to your computer and use it in GitHub Desktop.
How To Play Sound Effect In Particle System In Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Hellmade.Sound;
//Script used in https://www.youtube.com/watch?v=jKSz8JJnL4E
[RequireComponent(typeof(ParticleSystem))]
public class FireworksParticleSoundSystem : MonoBehaviour
{
private ParticleSystem _parentParticleSystem;
private int _currentNumberOfParticles = 0;
public AudioClip[] BornSounds;
public AudioClip[] DieSounds;
private ExplosionPhysicsForceEffect _explosionPhysicsForceEffect;
private Transform _playerTransform;
private void Awake()
{
_playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
// Start is called before the first frame update
void Start()
{
_parentParticleSystem = this.GetComponent<ParticleSystem>();
if(_parentParticleSystem == null)
Debug.LogError("Missing ParticleSystem!", this);
EazySoundManager.IgnoreDuplicateSounds = false;
_explosionPhysicsForceEffect = this.GetComponent<ExplosionPhysicsForceEffect>();
}
// Update is called once per frame
void Update()
{
var amount = Mathf.Abs(_currentNumberOfParticles - _parentParticleSystem.particleCount);
if (_parentParticleSystem.particleCount < _currentNumberOfParticles)
{
StartCoroutine(PlaySound(DieSounds[Random.Range(0, DieSounds.Length)], amount));
ApplyExplosionForceInArea();
}
if (_parentParticleSystem.particleCount > _currentNumberOfParticles)
{
StartCoroutine(PlaySound(BornSounds[Random.Range(0, BornSounds.Length)], amount));
ApplyExplosionForceInArea();
}
_currentNumberOfParticles = _parentParticleSystem.particleCount;
}
private IEnumerator PlaySound(AudioClip clip, int amount)
{
var distanceToPlayer = Vector3.Distance(this.transform.position, _playerTransform.position);
var soundDelay = distanceToPlayer / 343; //Speed of sound https://en.wikipedia.org/wiki/Speed_of_sound
//Debug.Log($"Distance to player '{distanceToPlayer}', therefore delayed sound of '{soundDelay}' sec.");
for (int i = 0; i < amount; i++)
{
//Debug.Log($"Play sound: '{clip.name}'");
int soundId = EazySoundManager.PrepareSound(clip, Random.Range(0.8f, 1.2f), false, this.transform);
var sound = EazySoundManager.GetSoundAudio(soundId);
sound.SetVolume(1.0f);
sound.Min3DDistance = 20;
sound.Max3DDistance = 250;
sound.SpatialBlend = 1f;
sound.Spread = 60f;
sound.DopplerLevel = 0f;
sound.Pitch = Random.Range(0.8f, 1.2f);
sound.RolloffMode = AudioRolloffMode.Custom;
//Realistic audio rolloff: https://forum.unity.com/threads/audio-realistic-sound-rolloff-tool.543362/
var animCurve = new AnimationCurve(
new Keyframe(sound.Min3DDistance, 1f),
new Keyframe(sound.Min3DDistance + (sound.Max3DDistance - sound.Min3DDistance) / 4f, .35f),
new Keyframe(sound.Max3DDistance, 0f));
animCurve.SmoothTangents(1, .025f);
sound.CustomCurve = animCurve;
StartCoroutine(PlaySound(sound, soundDelay));
//Attempt to avoid multiple of the same audio being played at the exact same time - as it sounds wierd
yield return new WaitForSeconds(0.05f);
}
}
private IEnumerator PlaySound(Audio sound, float delayInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);
sound.Play();
}
private void ApplyExplosionForceInArea()
{
if (_explosionPhysicsForceEffect != null)
_explosionPhysicsForceEffect.ApplyExplosionForce();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment