So, in order to do it, there are three simple steps to setting up the script. It really takes less than five minutes, if you already know all the objects you are going to call and use.
1. Setup the DaggerfallAudioSource as a general object property in your script: this will be used to load the audio control object and store it for attaching to other game objects. Drop it into your script properties area, like so.
Code: Select all
//sets up dfAudioSource object to attach objects to and play sound from said object.
DaggerfallAudioSource dfAudioSource;
Here, I setup the gameobject I'm going to attach it to in the general script properties area to ensure it can be accessed throughout the script object it is embedded in; this is fine because I'm attaching it to the player, which never changes gets removed during playing. If you are attaching it to dynamic objects, ensure to set it up wherever your setup the game object your creating and destroying.
Code: Select all
//used for attaching audio control object to and playing audio from.
GameObject player;
Code: Select all
void Start()
{
//finds the player and assigns them to the player objects.
player = GameObject.FindGameObjectWithTag("Player");
//finds daggerfall audio source object, loads it, and then adds it to the player object, so it knows where the sound source is from.
dfAudioSource = player.AddComponent<DaggerfallAudioSource>();
In my shield module, I needed sound feedback for raising shields and being hit. All I had to do call the object on a if-then check and tell it to play the equip clothing sound built into the engine, like so:
Code: Select all
//plays cloth equipping sound with a reduced sound level to simulate equipment rustling.
dfAudioSource.PlayOneShot(417,1,.3f);
Here is one more, more dynamic use of my dfAudioSource. On thing I found out quickly was using the same parry sound over and over for the hit sound made it feel very robotic and out dated. Nothing in reality sounds 100% alike. So, in order to resolve this, there was an easy solution. Combine the dfRand object with the dfAudioSource object to randomly select from the parry SoundClip list range, like so:
Code: Select all
dfAudioSource.PlayOneShot(DFRandom.random_range_inclusive(428, 436));
One last note, you can adjust the 3D audio simulation level unity has built into it by using the second input parameter "spatial blend". You can adjust the volume with the third input parameter. By combining and playing with these two inputs and numerous sound clips, you can create dynamic, unique sounds that will not be noticeable copies of each other, even though they are.