Table of Contents
Music, as a usually central and constantly active element of a soundscape, is a good candidate for using callbacks. In this lesson, callbacks have been integrated into the music to make graphical elements respond to the music themes. When receiving a notification from a musical feature, you can call elements in the game and make them seem like they are reacting to the music, like having the trees dance to the music rhythm.
The region music in the Wwise Adventure game consist of six different themes, each sharing the same tempo and key.
With the AkEvent or AkAmbient component it's a fairly easy process to set up callbacks. Both of these components will offer a Use Callbacks property, which is used to define the necessary information to create a callback.
To make a callback you need to define two types of information. First you will have to assign a Callback Function, and on what Game Object you will be able to find this function. Secondly, you need to specify when the Callback Function should be called, by selecting one or more Callback Flags (also referred to as Callback Type in scripts). These two pieces of information will be sent along with calling the event, and once the Callback Type is met, the function specified in the Callback Function property will be called. In the following steps, you will be modifying the scale of a tree based on the tempo of the music in the Wwise Adventure Game. First, you will post the Music_Region Event from an AkEvent component and secondly add the RhythmActions script to a tree.
Lastly, you will call the RhythmActions script from the Music_Region AkEvent by callback.
In the Unity menu, go to Audiokinetic > Certification > 301 > Lesson 7 then select Callbacks using Components.
The AkEvent posting the Music_Region Event can be added to any game object, as it is not being controlled by anything on a game object basis, and the music itself is not positioned. For ease of use, let's simply add the AkEvent to the Wwise game object.
In the Hierarchy, select the Wwise game object.
In the Inspector, click Add Component, then search for AkEvent and select it.
In the Event Name property, expand Events > Music > General and select Music_Region.
With the default setting of the Trigger On property, this is all you need to post the music at game initialization. To use callbacks, you must first declare what function (and thereby also the corresponding game object) the callback should be directed to. As such, let's start by adding the RhythmActions script to a tree in the Training Area.
In the Hierarchy, select the TrainingArea_Tree_01 game object.
In the Inspector, click Add Component, then search for the RhythmActionsscript and select it.
The RhythmActions script has been designed to influence the scale of the game object it's added onto. The Modifications class will allow you to set the scale of the game object by a certain factor on the X, Y, and Z axis.
You are welcome to change these slider values, but the default values should clearly give the impression of a breathing tree. Inside the RhythmActions script, a public function called 'PushActions' has been created.
This function will change the scale of the game object, depending on the slider values in the Modifications Class. It does so by gradually translating ("pushing") the Modification property settings into the Transform component and back to what it was before. Back in the AkEvent component on the Wwise game object, you can now add the TrainingArea_Tree_01 game object as a callback reference.
In the Hierarchy, select the Wwise game object, then expand the AkEvent and enable Use Callbacks.
To trigger the PushActions function from callbacks, you need to add the game object with the script added and write the exact function name.
Drag the TrainingArea_Tree_01 game object from the Hierarchy into the AkEvent's Game Object property.
In the Callback Function property, type PushActions.
Lastly, you need to add a Callback Flag. These flags, among other things, can refer to marker notifications in the Music Segments, like Beat, Bar, Entry, or Exit of the playing Music Segment. All, except the MusicSyncUserCue and Marker, are notifications that do not require any preparation in Wwise.
For this demonstration you will be using the MusicSyncBar notification, which is called each time the corresponding Event reaches any bar throughout its duration.
To get more information about each Callback Flag, please visit the Wwise SDK Documentation.
In the Callback Flags property drawer, select MusicSyncBar only.
That's it! You can play the game and watch the trees come alive.
Enter Play mode and move the Player close to the TrainingArea_Tree_01 game object, located towards the Village.
The tree will slowly move in and out like it's breathing. By connecting the RhythmActions script to the Music_Region Event, you made the tree grow to the rhythm of the music, thus creating a visual connection to the music. In addition, this method will also ensure that if your music is stopped or paused, the movement of the tree will not continue. This approach will also adapt to music tempo changes, so let's try to change the tempo as well.
In Wwise, click Remote…, highlight the Wwise Adventure Game (Editor), and click Connect.
In the toolbar's Search field, type "Music region", then click the Music_Region Switch Container.
In the Music_Regions Property Editor, drag the Playback Speed value to 2.5.
Switch back to Unity and look at the same tree.
Notice how the tree is still moving according to the rhythm of the music, but the cycle is noticeable faster since we increased the tempo of the music. If the Tree does not complete its cycle (increase and decrease in size fluidly), you can increase the Speed parameter on the RhythmActions script, so the movement will be able to complete before the next callback notification. You may exit Play mode.
Remember to set the Playback Speed back to 1 for any future exercises.
The AkEvent or AkAmbient components are the quickest way to set up callbacks with up to a few game objects. However, the method is not ideal when applied to many game objects because you'd have to add them one by one. Instead, you can get great benefits from enabling callbacks from a script, which you will learn more about in the next section.