-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroidFlight.cs
110 lines (81 loc) · 2.12 KB
/
AsteroidFlight.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using UnityEngine;
using System.Collections;
public class AsteroidFlight : MonoBehaviour {
public float flightSpeed;
public float growthMax;
public float growthSpeed;
public float despawnDepth;
public float despawnXScreen;
public float despawnYScreen;
public float threatToHumanLife;
public Vector3 baseScale;
float despawnRangeX;
float despawnRangeY;
bool grow = false;
Rigidbody rb;
GameObject player;
Camera cam;
AsteroidHandle handle;
Vector3 upScale;
void Start ()
{
if (!handle)
handle = GameObject.Find("Asteroider").GetComponent<AsteroidHandle>();
despawnRangeX = Screen.width * despawnXScreen;
despawnRangeY = Screen.height * despawnYScreen;
}
void OnEnable()
{
ReAquire();
transform.localScale = baseScale;
upScale = transform.localScale * (growthMax * Random.Range(0.3f, 3f));
grow = true;
Vector3 dangerVector = transform.position - player.transform.position;
rb.velocity = -dangerVector * flightSpeed;
}
void OnDisable()
{
grow = false;
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
void Update ()
{
if (grow)
{
transform.localScale = Vector3.Lerp(transform.localScale,
upScale,
Time.deltaTime * Time.deltaTime * growthSpeed);
}
/// Recycle
Vector3 screenPos = cam.WorldToScreenPoint(transform.position);
if (DetectScreenExit(screenPos))
{
float lives = Random.Range(0, threatToHumanLife * (Random.value * 10));
handle.livesSaved += lives;
gameObject.SetActive(false);
}
}
void ReAquire()
{
if (!rb)
rb = GetComponent<Rigidbody>();
if (!player)
player = FindObjectOfType<PlayerHandle>().gameObject;
if (!cam)
cam = player.GetComponentInChildren<Camera>();
despawnRangeX = Screen.width * despawnXScreen;
despawnRangeY = Screen.height * despawnYScreen;
}
bool DetectScreenExit(Vector3 pos)
{
if (Mathf.Abs(pos.x) > despawnRangeX
||
Mathf.Abs(pos.y) > despawnRangeY
||
transform.position.z > despawnDepth)
return true;
else
return false;
}
}