Game Development Reference
In-Depth Information
Audio Source Component as well to complete the setup. Now, start up
Game Mode and try it out. You will be able to run into the Door and not
pass through it, but the collision sound will still be able to play.
Under the Hood: More Advanced Audio Triggering Methods
The following examples are for more advanced and code-savvy individuals wishing to know more
specii cally about the various ways that Unity can trigger audio. This is not for beginners, but it gives a
more detailed look at the functioning of Unity's API and may be useful when explaining what you want
to have happen to a programmer or integrator.
More Methods of Triggering Audio and Scripting Terms in Unity
There are a number of methods to trigger audio in Unity, and some are more useful for certain purposes
than others are. Since version 4, a number of methods have been added that are not as well documented.
We'll try to give a basic description of the usage cases as well as test examples to try out. Note that all of
these methods will work with both Free and Pro versions:
First, a keyword is important: = audio here refers to a local Audio Source. If the script is placed on a Game
Object with an Audio Source, audio specii cally refers to that Audio Source. To refer to a specii c Audio
Source you can do the following:
This means set up the variable “myAudioSource” as a type of Audio Source:
var myAudioSource : AudioSource;
Play the sound (with optional delay time in parentheses (obsolete)):
myAudioSource.Play();
Any Audio Source must exist as a Component attached to a Game Object, so dei ning this variable means
you would either drop the Game Object with the Source you wanted on it, or specify exactly which Game
Object you want to use the Audio Source on. For now, we'll proceed locally.
audio.Play (<optional delay time>);
The simplest playback method, and the code equivalent to any Audio Source triggering an AudioClip, as
we showed with the ambience loop example earlier. audio.Play() will play whatever audio clip has been
selected, either by dropping it directly on the Audio Source Component, or specifying it in code with
audio.Clip, as in the following:
var sound;
audio.Clip = sound;
loop = true;
audio.Play();
Use audio.Play(); for continuous sounds like music or background ambiences. It isn't generally good at
retriggering sound ef ects. If Play() is invoked repeatedly on an Audio Source, the sound will interrupt
 
Search WWH ::




Custom Search