-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLaunchProjectile.js
51 lines (41 loc) · 1.31 KB
/
LaunchProjectile.js
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
#pragma strict
var projectilePrefab : Rigidbody;
var launchInterval : float = 10.0;
var randomTrajectory : boolean = false;
private var isLaunched : boolean = false;
private var audioSource: AudioSource;
// Starting in 2 seconds.
// a projectile will be launched every 5 seconds
// You can control whether projectile collides with world objects
// via Unity's physics collision matrix (put projectile on its own layer).
//function OnBecameVisible () {
// only launch invokeRepeating once
// if (isLaunched == false) {
// isLaunched = true;
// InvokeRepeating("LaunchProjectile", 0, launchInterval);
// }
//}
function Start () {
audioSource = GetComponent.<AudioSource>();
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("Player")){
if (isLaunched == false) {
isLaunched = true;
InvokeRepeating("LaunchProjectile", 0, launchInterval);
}
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.CompareTag ("Player")){
if (isLaunched == true) {
CancelInvoke("LaunchProjectile");
isLaunched = false;
}
}
}
function LaunchProjectile () {
var instance : Rigidbody = Instantiate(projectilePrefab, transform.position, transform.rotation);
if (randomTrajectory == true) {instance.velocity = Random.onUnitSphere * 5;}
if (audioSource) {audioSource.Play();}
}