Tanoda
AutoCannon.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections;
10using System.Collections.Generic;
11using UnityEngine;
12
13public class AutoCannon : MonoBehaviour {
14
15 public GameObject prefab;
16
17 public Transform spawnParent;
18 public Transform spawnLocation;
19 public float spawnSpeed;
20 public int spawnPeriod = 8;
21 public float lifeTime = 1;
22
23 public float rotationSpeed;
24 public float rotationFrequency;
25
26 public Queue<GameObject> pool = new Queue<GameObject>();
27
28 void Update() {
29 transform.Rotate(0, Time.deltaTime * rotationSpeed * (Mathf.PerlinNoise(rotationFrequency * Time.time, rotationFrequency * Time.time * 2) - 0.5f), 0);
30
31 if (Time.frameCount % spawnPeriod == 0) {
32 StartCoroutine(spawnCoroutine());
33 }
34 }
35
36 IEnumerator spawnCoroutine() {
37 GameObject obj;
38 if (pool.Count > 0) {
39 obj = pool.Dequeue();
40 } else {
41 obj = Instantiate(prefab);
42 obj.transform.SetParent(spawnParent);
43 }
44
45 obj.transform.position = spawnLocation.position;
46 obj.transform.rotation = spawnLocation.rotation;
47 obj.GetComponent<Rigidbody>().velocity = spawnLocation.forward.normalized * spawnSpeed;
48 obj.SetActive(true);
49
50 yield return new WaitForSeconds(lifeTime);
51
52 obj.SetActive(false);
53 yield return null;
54 pool.Enqueue(obj);
55 }
56}
float lifeTime
Definition: AutoCannon.cs:21
int spawnPeriod
Definition: AutoCannon.cs:20
Queue< GameObject > pool
Definition: AutoCannon.cs:26
float rotationSpeed
Definition: AutoCannon.cs:23
float spawnSpeed
Definition: AutoCannon.cs:19
Transform spawnParent
Definition: AutoCannon.cs:17
Transform spawnLocation
Definition: AutoCannon.cs:18
float rotationFrequency
Definition: AutoCannon.cs:24
GameObject prefab
Definition: AutoCannon.cs:15