Game Development Reference
In-Depth Information
to those variables. The script then destroys the pickup objects so the player cannot
exploit them by standing in the trigger.
var healthAdd : float = 50;
var ammoAdd : float = 1;
var bulletAdd : float = 17;
var ammoCollect : AudioClip;
var healthCollect : AudioClip;
function OnTriggerEnter(collisionInfo : Collider) {
if(collisionInfo.gameObject.tag == “Health”){
GameObject.Find(“Player”).GetComponent(FPSPlayer).hitPoints +=
healthAdd;
audio.PlayOneShot(healthCollect);
Destroy(collisionInfo.gameObject);
}
if(collisionInfo.gameObject.tag == “Ammo”){
GameObject.Find(“HandGun”).GetComponent(Handgun).extraBullets +=
bulletAdd;
GameObject.Find(“HandGun”).GetComponent(Handgun).bulletsLeft += ammoAdd;
audio.PlayOneShot(ammoCollect);
Destroy(collisionInfo.gameObject);
}
}
@script RequireComponent (AudioSource)
5. Add this script to the Player object. If Unity displays a message about losing the pre-
fab, then drag the Player object with the PlayerCollisions.js script to the prefab to
update it.
6. To enhance the visual effect of these objects, add a point light as a child of each—blue
for ammo and red for health—with a range of 7. Also have this light display a small
flare with the Flare setting (Figure 10.22).
7. Create another new script called PickupRotate.js in your Scripts folder. This script
makes the object to which it's attached rotate by three degrees every frame. Add this
code to the existing script by dragging the script to their Inspector views.
var rotationAmount : float = 3.0;
function Update () {
transform.Rotate(Vector3(0,0,rotationAmount));
}
8. Save each pickup as a prefab.
9. Save your Unity scene and project.
Search WWH ::




Custom Search