Game Development Reference
In-Depth Information
Listing 6.4 Picking up a Collided-with Object
var attachPoint: GameObject;
function OnControllerColliderHit (hit : Controller
if(hit.gameObject.tag == "key")
hit.gameObject.transform.parent =
function Update () {
Attach this script to the 3rd Person Controller in the Hierarchy.
The OnControllerColliderHit() function works in the same way as
OnCollisionEnter(). It is a specific collider function for the Character
Controller component that is part of the 3rd Person Controller.
Step 6. With the 3rd Person Controller selected, locate the script in
the Inspector. You will see the exposed value for attachPoint. This
is being used in the script as the transform to which the key will be
attached. Essentially, the avatar will pick up the key and it will appear
to be attached in the same location as the current wrench. If you
look through the hierarchy of the 3rd Person Controller you will see
that the wrench model is attached to Bip001 R Hand . Therefore, we
want this to be the parent of the key after the player has walked over
the key. As shown in Figure 6.7 , set the Bip001 R Hand object to the
attachPoint variable by dragging it from the Hierarchy onto the value
in the Inspector.
Step 7. Note that the key's collider is destroyed to stop it interfering
with the character's collider after it is picked up.
Step 8. Play. Walk the avatar over the key. It will be picked up—sort of.
It will be attached to the avatar and move in time with the wrench in
the avatar's hand. It just won't be aligned so nicely.
Step 9. To fix the alignment of the key, play the game and walk the
avatar over to pick up the key. Press pause. Switch to the Scene view.
Change the view around to zoom in on the key. Now translate, rotate,
and scale the key until it fits neatly in the avatar's hand, as shown in
Figure 6.8 . Write down the values for the RedKey transform in the
Inspector. Stop playing.
Search Nedrilad ::

Custom Search