-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroundCannon.cs
114 lines (110 loc) · 3.5 KB
/
GroundCannon.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
111
112
113
114
using UnityEngine;
using System.Collections;
public class GroundCannon : MonoBehaviour {
public GameObject cannonBullet, BulletSpawnPoint;
private GameObject player1, player2;
public float speed = 10f;
private int rng = 0;
private bool inrange = false;
public float range = 10f;
public float bulletRotation = 0f;
public float shootInterval = 2f;
public int numberOfShots = 1;
public float waitTime = 0f;
public bool groundCannon = false;
// Use this for initialization
void Start () {
player1 = GameObject.Find("Player");
player2 = GameObject.Find("Player2");
if (groundCannon)
{
InvokeRepeating("ShootCannon", 0, shootInterval);
}
else
{
StartCoroutine(Shoot());
}
}
// Update is called once per frame
void Update () {
float distance;
distance = Vector2.Distance(player1.transform.position, transform.position);
if(distance < range)
{
inrange = true;
Vector2 dir = player1.transform.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward);
Quaternion qto2 = Quaternion.Euler (qto.eulerAngles.x, qto.eulerAngles.y, qto.eulerAngles.z + 90);
transform.rotation = Quaternion.Slerp(transform.rotation, qto2, 5f * Time.deltaTime);
}
if(distance > range)
{
inrange = false;
}
}
IEnumerator Shoot()
{
yield return new WaitForSeconds(waitTime);
if (inrange)
{
if (MainMenu.player2)
{
rng = Random.Range(0, 2);
}
if (rng > 0 && player2 != null)
{
for (int i = 0; i < numberOfShots; i++)
{
Shot(player2);
yield return new WaitForSeconds(shootInterval);
}
}
/*if (player1 = null)
{
for (int i = 0; i < numberOfShots; i++)
{
Shot(player2);
yield return new WaitForSeconds(shootInterval);
}
}*/
else
{
for (int i = 0; i < numberOfShots; i++)
{
Shot(player1);
yield return new WaitForSeconds(shootInterval);
}
}
}
}
void ShootCannon()
{
if (inrange)
{
if (MainMenu.player2)
{
rng = Random.Range(0, 2);
}
if (rng > 0 && player2 != null)
{
Shot(player2);
}
else
{
Shot(player1);
}
}
}
void Shot(GameObject player)
{
GameObject pNewObject;
pNewObject = Instantiate(cannonBullet) as GameObject;
var trot = transform.rotation;
trot.x -= bulletRotation;
pNewObject.transform.rotation = trot;
Vector2 pos = new Vector2(BulletSpawnPoint.transform.position.x, BulletSpawnPoint.transform.position.y);
pNewObject.transform.position = pos;
pNewObject.GetComponent<Rigidbody2D>().velocity = (player.transform.position - transform.position).normalized * (speed*50);
}
}