Game Development Reference
In-Depth Information
Now we need to create a prefab for the rocket, in a process that reflects the power of core
reusability. First we need a shape to represent the rocking, and this shape is going to be
a cube with a scale of (0.1, 0.1, 0.75). The rocket has a behavior similar to the bullet: it
moves forward by a constant speed, hits the targets and destroys them. We can give these
abilities to the rocket by adding Projectile and TargetHitDestroyer scripts to it. One addi-
tional feature the rocket has is the ability to lock on a target and follow it. To implement
this feature, we add a third script to the rocket object. This third script is TargetFollower ,
shown in Listing 20.
1. using UnityEngine;
2. using System.Collections; 3.
4. public class TargetFollower : MonoBehaviour { 5.
6.
//Target we are following
7.
Target
currentTarget;
8.
9.
void Start () {
10.
//At the beginning we find the nearest target
11.
Target[] allTargets = FindObjectsOfType<Target>();
12.
13.
//Make sure there are targets in the scene
14.
if(allTargets.Length > 0){
15.
//Assume first target is the nearest
16.
Target nearest = allTargets[0];
17.
18.
//Find the nearest target
19.
foreach(Target t in allTargets){
20.
//We don't care about tar-
gets that have been
21.
//already hit
22.
if(!t.hit){
23.
//Distance between projectile
24.
//and
current target
Search WWH ::




Custom Search