Game Development Reference
In-Depth Information
09 //Reference to transform
10 private Transform ThisTransform = null;
11
12 //------------------------------------------------
13 //Called when object is created
14 void Start()
15 {
16 //Get First person capsule and make non-visible
17
MeshRenderer Capsule = GetComponentInChildren<MeshRenderer>();
18 Capsule.enabled = false;
19
20 //Get cached transform
21 ThisTransform = transform;
22 }
23 }
Line 17.
Here, the
GetComponentInChildren
API function is called to search
through the GameObject hierarchy, from the current object downward, to find
the first mesh renderer component. For First Person Controllers, there's only
one mesh renderer—the capsule collider. Line 18 disables the render, hiding the
collider.
Note
More information on
GetComponentInChildren
can be found in the Unity documentation at
Handling Cash Collection
Before proceeding, jump back to the previous chapter and consider Listing 4-11, especially lines
41 and 44. This sample implements the Cash Power-Up
OnTriggerEnter
event, which is called
each and every time the Player collides with the power-up in the scene. Lines 41 and 44 retrieve a
PlayerController component, attached to the
Player
GameObject, and increases its
Cash
member. In
other words, the PlayerController needs to implement a
Cash
variable to maintain its collected cash,
and this member should be increased for each Cash Power-Up collected. The Cash member should
be implemented as a
property
, and not a public variable. Doing this is consistent with the event-
handling functionality coded in Chapter 3, because properties allow us to
validate
the assignment
of values to variables, giving us the opportunities to call functions and invoke event notifications.
For cash collection, we'll need to verify whether the
collected cash
exceeds or meets the
total cash
available in the level. Remember, when the Player collects all available cash, the level is completed!
The
PlayerController
class can be amended to support cash collection using the code in Listing 5-4.