How do I add music to my Unity, Unreal, or Godot game?
Every game needs music. Even atmospheric and minimal games use gentle or simple tracks in the background. In-game music helps to immerse players and to improve the overall experience. In more advanced cases, you can use techniques to change the music based on what’s happening in your game.
In any case, it’s important to understand how to play music in your game. In this short guide, we’ll cover how to implement background music in the three major game engines: Unity, Unreal, and Godot.
If you need background music for your project, check out the Audio & Music category on OccaSoftware.
Without further ado, let’s get started.
Unity
- Import your audio file into the project.
- Create an empty GameObject in your scene.
- Add an Audio Source component to the GameObject.
- Drag your audio file onto the Audio Clip field in the Audio Source component.
- Check “Play On Awake” and “Loop” if you want the music to start automatically and repeat.
- Adjust volume and other settings as needed.
Godot
- Import your audio file into the project.
- Add an AudioStreamPlayer node to your scene.
- In the Inspector, set the Stream property to your imported audio file.
- Enable the Autoplay option if you want the music to start automatically.
- To loop the music, enable the Stream Loop option.
- Use GDScript to control playback if needed:
gdscriptCopy$AudioStreamPlayer.play()
Unreal Engine
- Import your audio file as a Sound Cue.
- In your level blueprint or a specific actor blueprint:
- Drag from an event node (e.g., BeginPlay) and search for "Play Sound".
- Select your Sound Cue in the Play Sound node.
- To loop the music, enable looping in the Sound Cue editor.
For more control, you can use the Audio Component:
- Add an Audio Component to your actor.
- Set the Sound property to your audio file.
- Call the Play method on the component when needed.
General tips for all game engines
In general, you’ll want to use the right fidelity and file size for your music tracks. Some file formats, like .wav or .flac, involve less compression. This means the track is higher quality, but it also means it takes up more disk space. This can affect the download time and bundle size for your game.
You’ll also want to offer players the ability to control the volume in-game. It’s so annoying when you load up a game and the background music blares through your speakers. Be sure to carefully balance the audio volumes and give players the opportunity to customize their audio levels.
If your game takes place in three dimensions, consider using spatial audio for your game. This can make the game feel incredibly immersive, with music bouncing off of walls and gunshots echoing through canyons. However, be aware that spatial audio often requires additional effort to implement. It also comes with a performance cost, so it can slow down your game.