-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShooter.cs
66 lines (55 loc) · 1.68 KB
/
Shooter.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
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public GameObject projectile, gun;
private GameObject projectileParent;
private Animator animator;
private AttackerSpawner myLaneSpawner;
void Start(){
animator = GameObject.FindObjectOfType<Animator>();
projectileParent = GameObject.Find("Projectile Parent");
if(!projectileParent){
projectileParent = new GameObject("Projectile Parent");
}
SetMyLaneSpawner();
}
void Update(){
// Shoot if attacker is ahead in lane otherwise stay out of attack state
SetMyLaneSpawner();
if(IsAttackerAheadInLane()){
animator.SetBool("isAttacking", true);
} else {
animator.SetBool("isAttacking", false);
}
}
void FireProjectile(){
GameObject newProjectile = Instantiate(projectile) as GameObject;
newProjectile.transform.parent = projectileParent.transform;
newProjectile.transform.position = gun.transform.position;
}
// Look through spawners and set myLaneSpawner
void SetMyLaneSpawner(){
AttackerSpawner[] spawnerArray = GameObject.FindObjectsOfType<AttackerSpawner>();
foreach(AttackerSpawner spawner in spawnerArray){
if(spawner.transform.position.y == this.transform.position.y){
myLaneSpawner = spawner;
return;
}
}
Debug.LogError(name + " No Spawner in Lane");
}
bool IsAttackerAheadInLane(){
// Exit if no attackers in lane
if(myLaneSpawner.transform.childCount <= 0){
return false;
}
// Check if attackers are ahead of defender
foreach(Transform child in myLaneSpawner.transform){
if(child.transform.position.x > transform.position.x){
return true;
}
}
// attacker present but not ahead of defender
return false;
}
}