-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenItems.cs
75 lines (69 loc) · 2.32 KB
/
GenItems.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenItems : MonoBehaviour
{
public Rigidbody prefab; //TODO: change to powerup or item class when class exists
private Vector3 spawnPos;
private Vector3 velocity;
private const float LIFETIME = 5.0f;
private const float SIZE = 40.0f;
private const float SIZE_RATE = 2.0f;
// Start is called before the first frame update
void Start()
{
spawnPos = this.transform.position;
spawnPos += this.transform.forward * 50; //will be 50m away
InvokeRepeating("createPowerUp", 0.5f, 2.0f);
}
// Update is called once per frame
void Update()
{
spawnPos = this.transform.position;
spawnPos += this.transform.forward * 50; //will be 50m away
}
void createPowerUp()
{
Quaternion rotate = Quaternion.Euler(Random.Range(-15.0f, 15.0f), Random.Range(-15.0f, 15.0f),0);
Rigidbody powerUp = Instantiate(prefab, new Vector3(spawnPos.x, spawnPos.y, spawnPos.z), Quaternion.Euler(0.0f, 90.0f, 0.0f));
powerUp.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
powerUp.velocity = rotate * (new Vector3(0f,0f,Random.Range(-20.0f, -10.0f)));
StartCoroutine(grow(powerUp.gameObject));
StartCoroutine(shrink(powerUp.gameObject));
StartCoroutine(destroyItem(powerUp.gameObject));
}
IEnumerator grow(GameObject obj)
{
for (float s = 0; s <= SIZE; s += SIZE_RATE)
{
if (obj != null)
{
obj.transform.localScale = new Vector3(s, s, s);
}
yield return null;
}
}
IEnumerator shrink(GameObject obj)
{
if (obj != null)
{
yield return new WaitForSeconds(LIFETIME - 0.5f);
for (float s = SIZE; s >= 0.0f; s -= SIZE_RATE)
{
if (obj != null)
{
obj.transform.localScale = new Vector3(s, s, s);
}
yield return null;
}
}
}
IEnumerator destroyItem(GameObject obj)
{
yield return new WaitForSeconds(LIFETIME);
if(obj != null)
{
Destroy(obj);
}
}
}