Game Development Reference
In-Depth Information
Breaking this down:
(1) public var victoryMusic : AudioClip;
Create a reference variable victoryMusic of type AudioClip. As a public
variable, you can easily test different audio clips by dragging and dropping
them into the Victory Music field in the Inspector.
1.
(2) function PlayVictoryMusic () {
Create a new PlayVictoryMusic() function.
2.
(3) if(audio.isPlaying == true) {
audio.Stop();
}
audio.clip = victoryMusic;
audio.Play();
3.
These are the same steps you performed earlier for swapping out audio clips,
only this time the new audio clip is the Fantasy Victory (free) audio clip.
Save the script. With the Audio game object selected in the Hierarchy, drag the Fantasy Victory (free)
audio clip from the Assets ➤ Free Pack folder in the Project panel into the Victory Music field of the
Background Music script component. Save the scene and save the project.
Run the player character around and you'll notice that when the player character enters the trigger
volume, the audio clip plays, which is what you want, but you'll also notice if the player character
runs away from the vicinity of the Fantasy Victory (free) game object, the music fades away. The
audio clip is set to 3D sound, with the Volume Rolloff set to Logarithmic Rolloff.
Select the Fantasy Victory (free) clip in the Project panel, then in the Inspector uncheck 3D Sound,
then Apply if prompted. Save the scene, playtest again, and now with 2D sound the music stays at
the same volume regardless of the player character's movement. Sweet victory!
Sound Effects
The term sound effect generally applies to audio clips that are not music or spoken dialogue. Sound
effects more commonly use 3D sound as they often correlate to specific game objects in the scene.
Open the Assets ➤ Sample Assets ➤ Vehicles ➤ Car ➤ Audio folder and find the Low Acceleration
audio clip. Select it and confirm that the 3D Sound box is checked in the Inspector. Select the
Enemy Drone game object in the Hierarchy, then move the cursor over to the Scene view and press
F to bring the Enemy Drone game object into view.
Drag the Low Acceleration audio clip from the Project panel and drop it onto the Drone. An Audio
Source component will be automatically created with the Low Acceleration file already assigned to
the Audio Clip property when it appears in the Inspector. In the Scene view, the audio source gizmo
consisting of a speaker symbol will also appear (Figure 11-18 ).
 
Search WWH ::




Custom Search