Section 5 – The Annoying Fly
Playing Positional Audio
For this tutorial we will make a fly that is controlled by the player. We will use the directional buttons
move the fly and as the fly moves we will hear the audio change position with the fly, getting quieter as it moves
away and louder as it approaches and when the fly is to the left or right of the listener the audio direction
will play accordingly.
First lets create a Game Object called "Viewpoint". Once created add a few actions to its creation event. First load
in the audio file called Fly.ogg which you can download from the bottom of the page. It's important to note that in order
to allow for sounds to have a 3D effect they must be Mono and not Stereo audio files. Use the load audio action to
load in the sound and give it a name such as FlySound.
Next create a new object called Fly which we haven't added to the game object list yet.
After that set the Viewpoint object as the Listener. To do this simply find the action called "Set Listnener" and
add it to the creation event, ensuring the Apply To selection is set to self.
Now we must configure how the 3D sounds behave relative to the listener.
Add an action called "Set Reference Distance" and give it a value of 5. This tells the game
how far away a sound must be for it to be at it's specified volume. If it is closer that 5 it will be louder and quieter if it
is further away.
Lastly we must set the rolloff factor. This tells the game how quickly the sound will get loud when it gets
closer than the reference distance. We will set this to 1.
Feel free to experiment with these values.
Now submit our new Viewpoint object and set it as our Init Object.
Next create our object to be the Fly. Create a new object and give it the name Fly.
set its loop audio property to true by ticking the loop audio checkbox.
On its creation event add a play positional audio action and input FlySound as the sound and the volume as 1.
On Up Pressed add an action to increase Z by 1. This will move the fly a step forward from the Viewpoint.
On DownPressed add an action to decrease Z by 1. This will move the fly a step backward from the Viewpoint.
On Left Pressed add an action to decrease X by 1. This will move the fly a step left from the Viewpoint.
On Right Pressed add an action to increase X by 1. This will move the fly a step right from the Viewpoint.
Now we can run the game and see that the Fly will move its position depending on what buttons we press and it will
get further and closer to the listener depending on how many times we press these buttons.