Adding Player Audio Effects

Hal Brooks
3 min readNov 25, 2021

Objective: Add sound effects for when player jumps, dies, swings sword, or picks up gem.

Create an empty game object named Audio Manager and add an AudioManager script to it. The player interacts with many game components, thus the AudioManager is set up to be a singleton, as shown below, so that it can be easily called from different scripts.

Create a variable for Player’s Audio Source component, _playerAudioEffects. And then create variables for the various AudioClips that will be used, as shown below.

Assign the audio clips in the inspector for the Audio Manager as shown below.

Variable assignment for AudioManager

A method for sword swing and a flaming sword swing is created. Each assigns the clip to the player’s audio source, i.e. _playerAudioEffects. The audio start time for the clip is set to zero. This is necessary to avoid audio clipping because a portion of the audio clips for jump are played using the same audio source, discussed later. The audio clip is then played.

SwingSword with and without Flame in AudioManager.

These audio methods are called when the animation is triggered using AudioManager.Instance, as shown below, dependent upon the GameManager.Instance.FlamingSword bool.

The audio clip for the Jump has two sound components, the jump and the landing. This entire clip is just under one second long.

Visual for jump audio showing timing of two sound events.

The jump and landing do not always occur with the same time spacing since the player is falling different distances. To solve this problem the ShortenJumpSound coroutine is started when the PlayerJump method is called, waiting for 0.3 seconds before stopping the audio. This is long enough to play the initial jump sound. The PlayerLands method starts the audio clip starts half way through to provide audio for the player landing.

The PlayerJump method is called when the player jump animation is started through the IEnumerator PlayerJumps, see below.

The PlayerLands method is called from the Jumping method in the PlayerAnimation script when the player is grounded following a jump. This combination of methods using the same audio clip allows appropriate timing for the landing sound following a jump.

Jumping method in PlayerAnimation script.

The methods for player death and picking up gems audio is shown below.

PlayerDied and GemPickup methods in AudioManager script.

The PlayerDied method in AudioManager script is called from the PlayerAnimation’s PlayerDies method at the same time as the death visual animation.

PlayerDies method on PlayerAnimation script.

The GemPickup method is accessed from the GetDiamond method on the Player script when the trigger a diamond.

This article shows how to control the beginning and ending of an audio clip using AudioSource clip, time, Play and Stop with a coroutine.

--

--