Game Development Reference
In-Depth Information
Lets move now to the other side of the collection process, and have a look at Collector
script. This script is shown in Listing 27.
1. using UnityEngine;
2. using System.Collections; 3.
4. public class Collector : MonoBehaviour { 5.
6.
//Radius of collision with collectables
7.
public float radius = 0.5f;
8.
9.
void Start () { 10.
11.
}
12.
13.
void Update () {
14.
//Get all collectables in the scene
15.
Collectable[] allCollectables =
16.
FindObjectsOfType<Collectable>();
17.
18.
//Check for collision with collectables
19.
//Use the radii to determine collision distance
20.
foreach(Collectable col in allCollectables){
21.
floatdistance=
22.
Vect-
or3.Distance(transform.position,
23.
col.transform.position);
24.
25.
//If the distance is less than the sum of the radii
26.
//Then we can try to collect this collectable
27.
if(distance < col.radius + radius){
Search WWH ::




Custom Search