Game Development Reference
In-Depth Information
1. using UnityEngine;
2. using System.Collections; 3.
4. public class TargetHitDestroyer : MonoBehaviour { 5.
6.
void Start () { 7.
8.
}
9.
10.
void Update () {
11.
//Find all targets in the scene
12.
Target[] allTargets = FindObjectsOfType<Target>();
13.
14.
//Check each target if it is hit
15.
foreach(Target t in allTargets){
16.
//We care about targets that havn't already been
hit
17.
if(!t.hit){
18.
//Distance between the pro-
jectile the target
19.
float distance = Vector3.Distance(
20.
transform.position,
21.
t.transform.position);
22.
23.
if(distance <
24.
t.transform.localScale.magnitude * 0.5f){
25.
//The projectile touches the target
26.
//Set hit flag to true.
27.
t.hit = true;
28.
29.
//Now destroy the projectile
30.
Destroy(gameObject);
Search WWH ::




Custom Search