Could anyone help me on how I would make a procedural fire audio script?
Hi guys,
I was just wondering if anyone could help me with this. I have a campfire within my scene and I would like to produce a script that will procedurally generate fire. I would be using an array of audio clips and would want these to be introduced procedurally. I am really struggling with this and I cannot see any tutorials online anywhere for procedural audio in Unity.
If anyone could help I would be so very grateful.
Answer by TBruce · Dec 13, 2016 at 12:22 AM
I am not sure what you are looking for. From what I understand you want to have the ability to play a list of sounds/clips sequentially and possibly even loop them when all sounds have played. Below is a script that will do just that
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MyAudioManager : MonoBehaviour
{
[Tooltip("List of AudioClip components. This list must be set to the sounds to be played and in the order of the list.")]
public List<AudioClip> audioClipList = new List<AudioClip>();
[Tooltip("The AudioSource component to play the AudioClips.")]
public AudioSource audioSource;
[Tooltip("AudioSource default. Only used if AudioSource is created at runtime.")]
public bool loopClips = true;
[Tooltip("Start playing sound immediately.")]
public bool playNow = true;
void Start ()
{
// make sure we have a reference to the AudioSource
if (audioSource == null)
{
if (audioSource == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
else
{
audioSource = gameObject.GetComponent<AudioSource>();
}
}
audioSource.playOnAwake = false;
audioSource.loop = false;
// make sure that alll the AudioClips in the audioClipList are valid by removing any null clips
audioClipList.RemoveAll(item => item == null);
if (playNow)
{
PlaySequentialSounds();
}
}
public void PlaySequentialSounds()
{
StartCoroutine(PlaySounds());
}
public void StopSounds()
{
StopCoroutine("PlaySounds");
}
IEnumerator PlaySounds()
{
if (audioClipList.Count > 0)
{
for (int i = 0; i < audioClipList.Count; i++)
{
audioSource.PlayOneShot(audioClipList[i]);
// wait for the lenght of the clip to finish playing
yield return new WaitForSeconds(audioClipList[i].length);
}
}
yield return null;
// if loopClips is set to true then call coroutine
if (loopClips)
{
StartCoroutine(PlaySounds());
}
}
}
If you set the variable loopClips
to true (the default) the clips will loop when the last clip has been played.
If you set the variable playNow
to true (the default) the clips will start to play immediately on startup.
If you do not want the clips to start playing automatically, you can call PlaySequentialSounds()
from another script as it is public. You can also stop sounds at anytime by calling StopSounds()
. (Note: You will need a reference to script to call either function PlaySequentialSounds()
or StopSounds()
. Or if you prefer you can make the class static.
I hope this helps.
This is the sort of idea I was looking for however I want it just to play random sounds at different pitches. For instance. Random burning wood sounds, then some sparking sounds and these would be randomly played within a certain range.
So basically I would have a main audio file which would be a fire sound which would loop. On top of this I would have sparks and small details which would be procedurally generated.
Thank you so much for the help.
That is not difficult. This class shown here does the following
It allows you to set a main looping sound/clip
It accepts a list of additional sound clips with varied volume levels for.
The additional sound clips are shuffled every time the list starts to play.
Enhancing the script should not be difficult at all if you feel the need to make additions/changes to it.
This is exactly what I was looking for! Thank you so much. There is one thing however whenever I untick Play Now or Play On Awake on the audio source the sounds still play anyways. I was wanting to only play the sounds if they were in a certain range but so just getting the transform of the fire and players position but I can't do that however has the sound just automatically plays
I am sorry but you were not clear about
I was wanting to only play the sounds if they were in a certain range but so just getting the transform of the fire and players position
and I could only guess. You will need to take the new code listed here and make any necessary modifications/changes/tweaks needed to meet your requirements as I feel that I have answered the original question fully and then some (If you need more you will need to post a new question using the new script).