Game Development Reference
In-Depth Information
Step 18: Create a new JavaScript. Name it
HallwayDoorsLockedTriggerScript . Open the script in your script editor.
Why?
I can hear you hard-core scripters screaming from here, “Just use the
HallwayDoorsTriggerScript and alter it to be able to check for a Boolean
state.” And you'd be absolutely right. A much more efficient method
would be to create one door-opening script that is useful in almost all
situations; however, we're going to build another to review how triggers
function, and to keep the script easy to read within the topic.
Step 19: Define variables for the door, the bulkheadHandle, a
doorOpenSound, the mainCamera, and the guiTextHints:
var door : GameObject;
var bulkheadHandle : GameObject;
var doorOpenSound : GameObject;
private var mainCamera : GameObject;
private var guiTextHints : GameObject;
Why?
The door is the object that opens. The bulkheadHandle is the wheel that
turns. The doorOpenSound is a sound that plays when this big ol' door
opens. The next two private variables allow us to go track down where
our state engine lives (attached to Main Camera), and drive a screen hint if
the player has not gotten the key yet.
Step 20: On Start, have the script populate mainCamera and guiTextHints:
function Start(){
mainCamera = GameObject.Find("Main Camera");
guiTextHints = GameObject.Find("GUITextHints");
}
Why?
So why make mainCamera and guiTextHints private variables and
populate them in script and not do this for the others? Good question;
the answer is that this will make the script applicable in other locked-
door situations. In most all situations where the door is locked (and we
want to be able to open it), the state engine will be accessed, and the
screen hints will be shown. Since these two objects won't change, hard
coding them saves populating in Unity. But since the door may indeed
change if this script is used elsewhere, keeping it a public variable (along
with the door handle and sound the door plays) will allow this script
more uses.
Search WWH ::




Custom Search