Day 25 — Aspiring Game Creator
2D Game Dev: Galaxy Shooter
Objective: Add background audio to Galaxy Shooter.
Pseudo Code or actually, the needed components:
- Audio Listener — component which “plays” all the audio through the laptop/computer’s audio device: laptop speaker, headphone, HDMI attached speaker. This component is usually attached to the Main Camera.
- Audio Source — component attached to a game object which will play the sound for that game object, though it’s actually piping that sound to the audio listener.
- Audio Clip — this is THE actual audio file.
Background music in three easy steps:
- Not much to this step, but important to check. The Main Camera game object has the required AUDIO LISTENER. If not, add the component.
- Create an empty game object: AudioManager, then a child object BackgroundMusic.
- Drop the BackgroundMusic track onto the BackgroundMusic game object. Ensure both Play On Awake and Loop are enabled, maybe turn the volume down a bit.
Dropping the music file directly onto the game object automatically adds the necessary Audio Source component and sets the Audio Clip too.
LET THERE BE SOUND! (just click play)
This is the absolute rudimentary, but workable way to get sound into the game, but there is another solution: Unity’s Audio Mixer which will be discussed in Day 26th’s article: How to Play Sound Effects in Unity.