Game Development Reference
In-Depth Information
5. Once it opens, delete whatever bit of script that already exists there.
We won't need it.
6. Then type the following code exactly:
var Sound : AudioClip;
function OnTriggerEnter(){
audio.PlayOneShot(Sound);
}
So, let's analyze what we've just typed within the context of
understanding the logic underlying the code:
Analyzing the Trigger Script
Line 1:
var Sound : AudioClip;
We begin by dei ning a variable known as Sound . The colon after “ Sound
can be read as “is a type of”. So this essentially means “dei ne a variable
called Sound that is a type of AudioClip”. Notice that we end the line
with a semicolon. This is extremely common practice in a number of
programming languages.
Line 2:
function OnTriggerEnter(){
The function OnTriggerEnter() is an existing function that is associated
with Collision events. A Collision event occurs when two objects with
Colliders touch each other. In this case, the Collision occurs when anything
encounters the Door. The Trigger is a special event of a Collider that is
easier for the system to process. It also means that the objects will actually
pass through each other rather than simply bump into each other.
Note the opening curly brace and the closing curly brace after the next
line. This is the conventional part of the code that says, “If an object
enters the trigger, then do whatever is within the curly braces.”
Line 3:
audio.PlayOneShot(Sound);
Here's where the sound plays! The audio.PlayOneShot(Sound) command
will play an audio i le one time completely. What sound will it play? It
 
Search WWH ::




Custom Search