Game Development Reference
In-Depth Information
Step 21: Have the script check to see if keyAcquired is true and if it is,
open the door.
function OnTriggerEnter (other:Collider){
if (mainCamera.GetComponent("AC_
ToolFunctionalityScript").keyAcquired){
iTween.RotateTo(door, Vector3(0,-110,0), 5.0);
iTween.RotateBy(bulkheadHandle, Vector3(3,0,0), 5);
doorOpenSound.audio.Play();
yield WaitForSeconds (doorOpenSound.audio.clip.
length);
Destroy (doorOpenSound);
Destroy (gameObject);
} else {
guiTextHints.guiText.text = "You need a key to open
this door.\nCheck the lock box near the entrance.";
iTween.MoveTo (guiTextHints, Vector3(.5,0,0), .5);
yield WaitForSeconds (3);
iTween.MoveTo (guiTextHints, Vector3(.5, -.1,0), 1);
}
}
Why?
So in plainspeak, the code says, “if the component 'AC_
ToolFunctionalityScript' that is attached to mainCamera says
that keyAcquired is true ( if(mainCamera.GetComponent( ' AC_
ToolFunctionalityScript ' ).keyAcquired){ ), then take the
door object and use iTween's RotateTo class and rotate it to Y =
-110 (iTween.RotateTo(door, Vector3(0,-110,0), 5.0); ).
Then spin the bulkheadHandle with iTween's RotateTo ( iTween.
RotateBy(bulkheadHandle, Vector3(3,0,0), 5); ). Play the
audio attached to doorOpenSound ( doorOpenSound.audio.Play(); ).
Wait for the length of the doorOpenSound audio, and then destroy
the sound and the gameObject this script is attached to ( Destroy
(doorOpenSound) ; and Destroy (gameObject) ).”
Warnings and Pitfalls
If the door isn't opening,
double-check to make
sure that it isn't still
marked as Static (left over
from the baking process).
This always trips me up
because I want to make
sure doors are included
in the baking, but I want
to be able to control
them later on in the
process via script. Gotta
make sure and get Static
turned off for items that
won't be, well … static.
But if keyAcquired is not true ( else { ), then change the text on the
guiText component of the guiTextHints GameObject to read, “You need
a key to open this door (and then on a second line). Check the lock box
near the entrance.” Then move the guiTextHints object up with iTweens
MoveTo, wait for 3 seconds and then move it back down.
Tips and Tricks
Note that the \n is not a typo. Without spaces, this tells Unity to split the
string we've used to define the guiText's text into two lines.
Step 22: Save and return to Unity. Fix syntax problems.
Search WWH ::




Custom Search