Game Development Reference
In-Depth Information
Tutorial 15.1: The Power of Prefabs
Step 1 : Open Scene-Hallway.
Step 2: Deactivate Hallway_Unbaked and activate the Hallway_BakedGroup
(and all its children). Remember, to do this, just select the object in the Hierarchy
panel, and then check (or uncheck) the box next to the object's name.
Why?
When the game is run we'll eventually want the baked version to be
turned on later, but the CCTV camera will only be active when the power
is on, and thus we want to build the functionality in as though the scene
has already turned on the lights.
Step 3: Create a new JavaScript and name it CCTV-
CameraSearchingScript . Open it in your script editor.
Step 4: Create the raycasting mechanism. Start with this familiar code:
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position,transform
.forward, hit, 100)){
Debug.DrawLine (transform.position, hit.point,
Color.yellow);
}
}
Why?
Everything there should be familiar—it literally is the same code as used
in the last chapter. The only variation is the Color.yellow part. This is
just coloring the debug ray so it's easy to see and differentiate from any
other rays that might be cast in the scene.
Step 5: Save and return to Unity. Fix any syntax errors that pop up in the
Console.
Step 6: Apply this CCTV-CameraSearchingScript to any of the camera's
Hallway_CCTV_Led GameObjects in the Hierarchy (Hallway_CCTV_
Group/Hallway_CCTV_Prefab/Hallway_CCTV_Camera/Hallway_CCTV_
Led). When a new script is added to the prefab, Unity will warn that the
prefab connection is about to be broken. Go ahead and click Continue.
Step 7: Apply this change to all the CCTV_Camera_Prefabs in the scene.
To do this, in the Hierarchy just select the prefab to which the script was
added (Hallway_CCTV_Led), and in the Inspector, click the Apply button
that is on the Prefab line of buttons.
Step 8: Test. Play the game and take special note of what's happening in
the Scene window ( Figure 15.6 ).
Search WWH ::




Custom Search