-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissleScript.cs
More file actions
420 lines (272 loc) · 12.2 KB
/
MissleScript.cs
File metadata and controls
420 lines (272 loc) · 12.2 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class MissleScript : MonoBehaviour
{
/*This script will (hopefully) prepare you to get used to Unity's formatting and system for objects.
"gameObject" refers to the current object the script is on, while "GameObject" refers to the GameObject Object.
Parent-Child Hierarchy: gameObject -> transform -> position, etc
Vector3 = object type containing 3 float values, x,y, and z.
Vector2 = object type containing 2 float values, x and y.
Examples: gameObject.transform.position = new Vector3(0,0,0);
if you use "transform" without the gameObject before it, it defaults to the object the script is attached to.
gameObject.transform.position = new Vector3(0,0,0); works the same as: transform.position = new Vector3(0,0,0);
Some commonly used functions/methods between many scripts.
GameObject.Find("string"); returns the GameObject with the name "string". Use only to find objects that only have one instance and constantly stay loaded. Returns null if object cannot be found. (Will probably cause errors if object cannot be found).
Destroy(gameObject); takes in a GameObject as an argument, and removes it from the scene, killing all active components and scripts on the object.
Invoke("string", 0.5f); Runs the function with the name "string", after the amount of seconds in second argument. (For the example, the line will run the function called string after 0.5 seconds).
Instantiate(gameObject, transform.position, Quaternion.identity); Spawns the GameObject defined in the first argument, at the Vector3 of the second argument, in the orientation of the Quaternion given in the thrid argument.
*/
//Instance variables (can be defined within script or out of script in the Unity editor)
public float speed;
public float secondSpeed;
public float upSpeed;
public Transform crosshairTransform;
public GameObject crosshairObj;
public GameObject crosshair;
public GameObject player;
public GameObject rootShoot;
private Vector3 target;
public float vary;
public float damage;
public float angleOfShot;
public bool friendly;
public float index;
public float timeToStop;
public bool hasFuse;
public bool enemyOnly;
public float pulses;
public float currentPulse;
public float pulseDelay;
public bool canBeHeld;
public bool inMotion;
public proxyCursor proxycursor;
public float currentFuseTime;
public float fuseTime;
public float currentControlTime;
public float controlTime;
public bool selfDamaging;
public float selfDamageAmount;
public bool released;
public ContactPoint2D[] contactPointArray;
public List<ContactPoint2D> uniqueContacts;
public float blastRadius;
public LayerMask damagables;
public float lifetime;
public float freezeFrameDuration;
public float screenShakeMag;
public float screenShakeDuration;
public GameObject soundSample;
public AudioClip detonationSound;
public float detonationVolume;
public GameObject lingerEffect;
//GameObjects of the bullet collision particles, and damage numbers.
public GameObject bulletDeath;
public GameObject bulletDeathEnemy;
public GameObject bulletNumber;
//The bullet's animator component.
public Animator anim;
//Runs at the beginning of bullet existence.
void Start()
{
angleOfShot = GameObject.Find("RootShoot").GetComponent<Shoot>().currentGun.transform.rotation.eulerAngles.z;
//initial variables
crosshair = GameObject.Find("Crosshair");
//The GameObject instance variable "crosshairObj" is defined as the GameObject found with the name "CrosshairProxy".
crosshairObj = GameObject.Find("CrosshairProxy");
//The instance variable Transform object "crosshairTransform" is defined as the transform of crosshairObj.
crosshairTransform = crosshairObj.transform;
//The GameObject instance variable "player" is defined as the GameObject found with the name "Player".
player = GameObject.Find("Player");
rootShoot = GameObject.Find("RootShoot");
proxycursor = GameObject.Find("CrosshairProxy").GetComponent<proxyCursor>();
//The transform.right is one of the orientation's of a GameObject. This line points this object's transform.right orientation towards the crosshair.
transform.right = crosshairTransform.position - transform.position;
//Add random rotation based on the "vary" float variable.
transform.Rotate(0, 0, Random.Range(-vary, vary));
rootShoot.GetComponent<Shoot>().swapEnable = false;
rootShoot.GetComponent<Shoot>().shootEnable = false;
/*
if(Vector2.Distance(transform.position,player.transform.position) < 0.5f)
{
}
*/
upSpeed = speed;
if (timeToStop != -1 && !canBeHeld)
{
LeanTween.value(gameObject, speed, 0f, timeToStop).setOnUpdate((float val) => {
upSpeed = val;
});
}
released = false;
if (hasFuse)
{
currentFuseTime = fuseTime;
}
currentControlTime = controlTime;
//removes this object from existance.
Destroy(gameObject, lifetime);
}
void Update()
{
//The position of the gameObject adds the Vector value of transform.right multiplied by speed and the change in time.
if(Input.GetKey(KeyCode.Mouse0) && !released)
{
transform.right = crosshair.transform.position - transform.position;
}
if ((Input.GetKeyUp(KeyCode.Mouse0) || currentControlTime <= 0) && !released)
{
released = true;
if(secondSpeed != 0)
{
upSpeed = secondSpeed;
}
}
transform.position += transform.right * upSpeed * Time.deltaTime;
if (hasFuse)
{
currentFuseTime -= Time.deltaTime;
}
if(currentControlTime > 0)
{
currentControlTime -= Time.deltaTime;
}
if (hasFuse && currentFuseTime <= 0)
{
hasFuse = false;
StartCoroutine(Detonation());
}
//destroy this object after 3 sec
}
public IEnumerator Detonation()
{
yield return new WaitForSeconds(pulseDelay);
rootShoot.GetComponent<Shoot>().swapEnable = true;
rootShoot.GetComponent<Shoot>().shootEnable = true;
currentPulse++;
Collider2D[] enemies = Physics2D.OverlapCircleAll(transform.position, blastRadius, damagables);
Instantiate(bulletDeath, transform.position, Quaternion.identity);
foreach (Collider2D en in enemies)
{
//print(en.gameObject.name);
if (en.gameObject.tag == "Enemy")
{
if (en.gameObject.GetComponent<EnemyBehaviour>() != null)
{
en.gameObject.GetComponent<EnemyBehaviour>().TakeDamage(damage, index);
}
if (en.gameObject.GetComponent<MeleeEnemyBehaviour>() != null)
{
en.gameObject.GetComponent<MeleeEnemyBehaviour>().TakeDamage(damage, index);
}
if (en.gameObject.GetComponent<ClapEnemyBehaviour>() != null)
{
en.gameObject.GetComponent<ClapEnemyBehaviour>().TakeDamage(damage, index);
}
var dmgNumber = Instantiate(bulletNumber, en.gameObject.transform.position, Quaternion.identity);
dmgNumber.GetComponent<BulletNumber>().SetVal(damage);
if (GetComponent<Rigidbody2D>().velocity.x > 0)
{
dmgNumber.GetComponent<BulletNumber>().right = false;
}
else
{
dmgNumber.GetComponent<BulletNumber>().right = true;
}
//Destroy(gameObject);
}
if (selfDamaging && en.gameObject.tag == "Player")
{
en.gameObject.GetComponent<PlayerBehaviour>().TakeDamage(selfDamageAmount);
}
if (!enemyOnly && (en.gameObject.tag == "Wall" || en.gameObject.tag == "SolidProp"))
{
//print "WALL!" (for testing).
//Spawn an instance of the bulletDeath gameobject at this object's transform's position.
/*
var dmgNumber = Instantiate(bulletNumber, transform.position, Quaternion.identity);
dmgNumber.GetComponent<BulletNumber>().SetVal(damage);
*/
if (en.gameObject.tag == "Wall")
{
if (en.gameObject.GetComponent<wall>() != null)
{
en.gameObject.GetComponent<wall>().TakeDamage(damage);
}
}
}
if (!enemyOnly && en.gameObject.tag == "Destruct")
{
en.gameObject.GetComponent<DestRock>().TakeDamage(damage, true);
/*
var dmgNumber = Instantiate(bulletNumber, transform.position, Quaternion.identity);
dmgNumber.GetComponent<BulletNumber>().SetVal(damage);
*/
}
if (en.gameObject.tag == "CliffCollide")
{
if (index != 7)
{
foreach (ContactPoint2D col in uniqueContacts)
{
}
}
/*
var dmgNumber = Instantiate(bulletNumber, transform.position, Quaternion.identity);
dmgNumber.GetComponent<BulletNumber>().SetVal(damage);
*/
}
}
if (pulses > 0)
{
if (currentPulse == pulses)
{
if (lingerEffect != null)
{
Instantiate(lingerEffect, transform.position, Quaternion.identity);
}
Destroy(gameObject);
}
else
{
StartCoroutine(Detonation());
}
}
else
{
GameObject.Find("LevelController").GetComponent<PauseMenu>().FreezeFrame(freezeFrameDuration);
if(transform.GetChild(0).GetComponent<ParticleSystem>() != null)
{
transform.GetChild(0).GetComponent<ParticleSystem>().Stop();
transform.GetChild(0).parent = null;
}
if (detonationSound != null)
{
var sound = Instantiate(soundSample, transform.position, Quaternion.identity);
sound.GetComponent<SoundSample>().SpawnSound(detonationSound, 0f, detonationVolume);
}
if (GameObject.Find("Cam") != null && screenShakeDuration != 0 && screenShakeMag != 0)
{
GameObject.Find("Cam").GetComponent<CameraShake>().ShakeIt(screenShakeMag, screenShakeDuration);
}
if (lingerEffect != null)
{
Instantiate(lingerEffect, transform.position, Quaternion.identity);
}
Destroy(gameObject);
}
}
//runs when 2D collider on bullet collides with something. The collider the bullet collides with is definied as "other".
void OnCollisionEnter2D(Collision2D other)
{
if (pulses <= 0)
{
StartCoroutine(Detonation());
}
}
void BulletDeath()
{
Destroy(gameObject);
}
}