-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpawner.cs
59 lines (50 loc) · 1.45 KB
/
Spawner.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
57
58
/// <summary>
/// Spawner.cs
/// Author: MutantGopher
/// This is a sample spawning script used to spawn the red cubes in the demo scene.
/// </summary>
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public GameObject prefabToSpawn; // The prefab that should be spawned
public float spawnFrequency = 6.0f; // The time (in seconds) between spawns
public bool spawnOnStart = false; // Whether or not one instance of the prefab should be spawned on Start()
public bool move = true; // Move this spawn spot around
public float moveAmount = 5.0f; // The amount to move
public float turnAmount = 5.0f; // The amount to turn
private float spawnTimer = 0.0f;
// Use this for initialization
void Start()
{
if (spawnOnStart)
{
Spawn();
}
}
// Update is called once per frame
void FixedUpdate()
{
// Update the spawning timer
spawnTimer += Time.deltaTime;
// Spawn a prefab if the timer has reached spawnFrequency
if (spawnTimer >= spawnFrequency)
{
// First reset the spawn timer to 0
spawnTimer = 0.0f;
Spawn();
}
// Move and turn so that boxes don't keep spawning in the same spots
transform.Translate(0, 0, moveAmount);
transform.Rotate(0, turnAmount, 0);
}
void Spawn()
{
// First check to see if the prefab hasn't been set
if (prefabToSpawn != null)
{
// Instantiate the prefab
Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
}
}
}