Game Development Reference
In-Depth Information
Step 23: Add the mechanism to destroy both the sound and the trigger.
For HallwayDoorsTriggerScript add (in italics):
var door : GameObject;
var doorOpenSound : GameObject;
function OnTriggerEnter (other:Collider){
iTween.RotateTo(door, Vector3(0,-120,0), 5.0);
doorOpenSound.audio.Play();
Destroy (doorOpenSound);
Destroy (gameObject);
}
Why?
The first destroy destroys the GameObject that contains the sound (that
we declared at the top of the script and populate in the editor). The
second destroy destroys the GameObject that this script is attached to,
which in this case is the trigger.
Step 24: Make the destroy wait—specifically wait for the duration of the
sound clip before destroying things. Add the following line:
var door : GameObject;
var doorOpenSound : GameObject;
function OnTriggerEnter (other:Collider){
iTween.RotateTo(door, Vector3(0,-120,0), 5.0);
doorOpenSound.audio.Play();
yield WaitForSeconds (doorOpenSound.audio.clip
.length);
Destroy (doorOpenSound);
Destroy (gameObject);
}
Why?
Without our yield statement, the sound wouldn't be heard at all. Looking
at the script in step 21 shows a script that destroys the sound object
immediately after telling it to play—thus the sound would be gone before
we got a chance to enjoy it. Yield in JavaScript means “wait” or “allow
something to finish first.” In this case we are using yield to wait for a specific
duration of time (WaitForSeconds). The seconds that this waits for can be
defined with a simple integer (for instance: yield WaitForSeconds (3); would
wait for three seconds before executing the next step). However, we want
this script to be more flexible so it can be used with different situations and
sounds. So we tell it to check out the length of the audio clip attached to
doorOpenSound (doorOpenSound.audio.clip.length) and wait that long.
Search WWH ::




Custom Search