-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSpawnGameObjects.cs
56 lines (47 loc) · 1.55 KB
/
SpawnGameObjects.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using UnityEngine;
using System.Collections;
/// <summary>
/// A generic game object spawner class.
/// </summary>
public class SpawnGameObjects : MonoBehaviour {
// The prefab of the game object to spawn.
public GameObject spawnPrefab;
// The spawn time internal. The object is spawned randomly under this interval.
public float minSecondsBetweenSpawning = 3.0f;
public float maxSecondsBetweenSpawning = 6.0f;
// Set this if the spawners should chase another game object.
public Transform chaseTarget;
private float savedTime;
private float secondsBetweenSpawning;
/// <summary>
/// Use this for initialization
/// </summary>
void Start () {
savedTime = Time.time;
secondsBetweenSpawning = Random.Range (minSecondsBetweenSpawning, maxSecondsBetweenSpawning);
}
/// <summary>
/// Update is called once per frame
/// </summary>
void Update () {
// Check if it is the time to spawn again.
if (Time.time - savedTime >= secondsBetweenSpawning)
{
MakeThingToSpawn();
savedTime = Time.time; // store for next spawn
secondsBetweenSpawning = Random.Range (minSecondsBetweenSpawning, maxSecondsBetweenSpawning);
}
}
/// <summary>
/// Create and setup the spawned game object.
/// </summary>
void MakeThingToSpawn() {
// Create a new gameObject
GameObject clone = Instantiate(spawnPrefab, transform.position, transform.rotation) as GameObject;
// Set chaseTarget if specified
if ((chaseTarget != null) && (clone.gameObject.GetComponent<Chaser> () != null))
{
clone.gameObject.GetComponent<Chaser>().SetTarget(chaseTarget);
}
}
}