Game Development Reference
In-Depth Information
sort of prefab that can then be dragged up into the Hierarchy and used. If
any changes are made to the core GameObject, all the prefabs will update
to reflect this change. Especially when bouncing back and forth between
Maya and Unity, keeping a prefab connection can be of immense help
because the updates made in Maya will automatically propagate into the
scene when the new .fbx is brought in. However, in this case, where there
is only going to be one First Person Controller in this scene, it is of no
concern that we lose this prefab connection; go ahead and break it.
Step 17: Try this shortcut. From the Project panel, drag the footstep
audio clip up and drop it onto the Sound-Footsteps GameObject. This will
“automagically” add an Audio Source component to the GameObject. It's
really the same as using the Component>Audio>Audio Source method—
just quicker.
Step 18: Make sure the audio clip is set to Play on Awake and to Loop.
Remember both of these options are available in the Inspector in the
Audio Source component of the Sound-Footsteps GameObject.
Step 19: Play the game and adjust volume. It will be a little weird since
right now, when the game is played, the footsteps will be running all
the time. Ignore this little glitch and dial in the right volume for the
sound clip. We will make it so that the sound isn't continuously running
in a bit.
Scripting Sound
So we now have sound that we walk past. And we have sound that sticks
with us as the player as we walk through the scene. Now we need to take a
moment and make this sound only play on demand—when we're moving.
The process here will go like this: first, we'll make the Audio Source inactive so
that it doesn't play. Then we'll construct a simple script that will activate the
Audio Source when the player is moving; or more precisely, when the keys
that move the character are pressed.
Step 20: Deactivate Sound-Footsteps' Audio Source component. Do this
by selecting Sound-Footsteps in the Hierarchy panel. Then in the Inspector
panel, click off the Audio Source component. Be sure to leave Play on
Awake and Loop still checked.
Step 21: Create a new JavaScript. In the Project panel, choose
Create>JavaScript. This can also be done via Assets>Create>JavaScript.
A new icon will appear in the Project panel called NewBehaviourScript
(Unity is built by Europeans, thus the funky spelling). Note that the
icon next to it has a little JS on it for “JavaScript.” Rename this script
SoundFootstepsControl .
Step 22: Open the script. Do this by double-clicking the script in the
Project panel. This will open another application. By default on a PC it will
be UniSciTE and on a Mac it will be Unitron. There you will be presented
with the basics of a script ( Figure 11.5 ).
Search WWH ::




Custom Search