itch.io is community of indie game creators and players

Devlogs

Lance Harrington - Milestone One Part Two: Audio System

Iron Echos: Zenith's Uprising
A downloadable game for Windows, macOS, and Linux

Lance Harrington

Milestone One Part Two: Audio System

 

                As mentioned in Milestone One Part One: Enemy AI, the second part of my first milestone was to add an audio system to the game that can be used by anybody in their scripts with ease. I will be going through how I developed this and the bonus perks of doing it this way. All the users will need is access to an Audio Clip and one line of code. The sound system is comprised of three scripts, some GameObjects for those scripts, a Sound Mixer component, a panel for a canvas, and some Audio Clips.

                Starting off, the threes Scripts are a SoundFXManager, Musicmanager, and SoundMmixerManager. MusicManager. These three scripts are placed on game objects in the scene. The music manager script is intended to handle multiple background audio clips, but currently is just a placeholder for now and is just set up for 1 background song. The SoundFXManager however is set up, but will take about that separately. Just know that the SoundFXManager and Music Manager are both using a sound mixer to control their volume. This was intended to allow the player to be able to control the volume in a menu. The three options are Master Volume, Sound FX Volume, and Music Volume:


The UI is basic right now, but I will get with the UI guy about it soon.

                Now onto the SoundFXManager, which allows for an object to me instantiated at a position of our choosing, play a sound, and destroy the game object immediately after the sound is finished. Before I did the code however, I created a game object in the hierarchy and called it SoundFXObject. I added an Audio Source component to the object, turned off the play on awake feature,  and connected its output to our mixer. I then made this object into a prefab and deleted it from the hierarchy. This will be the object we instantiate that plays the audio clip and then destroys itself.

Next up is the code:

                There are two serialized fields that we have with SoundFXManager. The first field is for our SoundFXPrefab that was made previously, and the second field is for the volume mixer in case we need it later on.  Also, at the top you will see the static variable of a SoundFXManager named Instance, which is then set to this instance of SoundFXManager in start, which will allow us to reference this script by typing “SoundFXManager.Instance” in any script in our program. This should be the only one and I will add a more concrete singleton later to keep multiples from being made.

                Now all we need is the method to play the clip we want, which is exactly what the public method PlaySoundFXClip does. This method takes in three parameters being an audio clip, a transform, and a float. Currently, whatever script that calls this method will have to have brought in the audio clip with a serialized field to pass into the method. The transform is used to get the position of where we want to spawn the soundFXObject. The volume is used to set the volume of the audio clip and should be a number between 0-1.

                Once we have all the components to pass in, we start out by creating a reference to the audio source. By setting audio source equal to our instantiation, the audio source on the prefab will automatically be set to the variable audio source.  Next, we add the audio clip to the source and alter the volume on the source. We then play the audio source and get its length. Finally, we call GameObject.Destroy and destroy our audio source game object after the length of the clip has passed. Heres what it looks like when called in one of the enemy state scripts

                All in all, I would say that I have a basic audio setup that anyone can use to play sound effects. In the future I will work on the music manager, which will play different background music based on location and what’s going on in the scene. Between Part 1 and Part 2, I believe that I met my milestone goals and will be surly to expand upon these components as I get through more milestones.

Download Iron Echos: Zenith's Uprising
Leave a comment