Game Development Reference
In-Depth Information
Figure 11.5 Basics of a script as presented in UniSciTE.
Step 23: Enter the following text:
function Update () {
if (Input.GetButton(“Vertical”) ||
Input.GetButton(“Horizontal”)){
audio.enabled = true;
}
else {
audio.enabled = false;
}
}
Tips and Tricks
Notice that there is that strange || symbol. That is the script symbol for
“or” and is generally made by pressing Shift-\ (the key right beneath the
Backspace key) on US keyboards. Also note that the line that begins with
"if (Input.GetButton)" is all one line and doesn't end until the "{".
Why?
OK, I know I said we weren't going to go into great depth about what was
happening here, but it's worth a quick look.
“function Update” just means “check every frame.” So the script tells Unity
to check every frame “if” the player is pushing the Vertical or Horizontal
buttons. And if he is, it sets the audio component to be enabled (audio.
enabled=true;), which means the audio is playing. If the player is not
pushing either of these buttons (else), it sets the audio component to
not be enabled (audio.enabled=false;) and thus turns the sound off.
Vertical and Horizontal are two inputs that are already defined in the
Input section of Unity (Edit>Project Settings>Input). Don't worry too
much about this for now, but Horizontal are the strafing A and D keys, and
Vertical are the W and S keys. So when W, A, S, or D are pressed, the audio
clip is enabled and the sound plays.
Step 24: Save the script (File>Save). When you return to Unity, open
the Console (Window>Console) where Unity will tell you of any obvious
syntax errors. If you find any errors, Unity will give point toward the line
number where the error exists. Go back and correct.
Search WWH ::




Custom Search