-
Notifications
You must be signed in to change notification settings - Fork 0
/
zombieAI.cs
254 lines (226 loc) · 8.68 KB
/
zombieAI.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
//programmed by Ewan McElroy
public class zombieAI : MonoBehaviour
{
#region variables
private int nodeIndex = 0;
[SerializeField] private float maxSpeed = 0.01f;
[SerializeField] private float nodeRadius = 1f;
[SerializeField] private float soundDist = 1.0f;
[SerializeField] private float maxCOuntDown = 3;
[SerializeField] private float rayDist = 3;
[SerializeField] private float wanderDist;
[SerializeField] private float maxWanderCount = 2;
[SerializeField] private float seekDist = 1.0f;
private float counter;
private float wanderCounter;
private Rigidbody rb;
[SerializeField] private GameObject[] nodes;
private GameObject player;
private GameObject zombieClosest;
private GameObject playerClosest;
private List<GameObject> path = new List<GameObject>();
//private List<GameObject> pickList = new List<GameObject>();
//private List<GameObject> potentialDirection = new List<GameObject>();
private bool pathFound;
private Vector3 wanderLocation;
#endregion
[HideInInspector] public enum aiState // ai Enum
{
IDLE,
PATHFIND,
FOLLOWPATH,
CHASEPLAYER
};
[HideInInspector] public aiState behaviourState = aiState.IDLE;
private void Start()
{
// ininitialisation
rb = GetComponent<Rigidbody>();
player = GameObject.FindGameObjectWithTag("Player");
nodes = GameObject.FindGameObjectsWithTag("Nodes");
counter = maxCOuntDown;
wanderCounter = maxWanderCount;
StateChecker();
}
void StateChecker()
{
switch (behaviourState)
{
case (aiState.IDLE):
{
idle();
break;
}
case (aiState.PATHFIND):
{
Debug.Log("Pathing!");
nonNavMesh();
break;
}
case (aiState.FOLLOWPATH):
{
seek(path[nodeIndex].transform.position);
break;
}
case (aiState.CHASEPLAYER):
{
counter -= Time.deltaTime;
if (counter <= 0 && Vector3.Distance(transform.position, player.transform.position) > seekDist)
{
counter = maxCOuntDown;
path.Clear();
pathFound = false;
behaviourState = aiState.PATHFIND;
}
seek(player.transform.position);
break;
}
}
Invoke("StateChecker", 0.0f);
}
private void idle()
{
if(player.GetComponent<AudioSource>().isPlaying && Vector3.Distance(player.transform.position, transform.position) < soundDist) // if a sound plays
{
behaviourState = aiState.PATHFIND; // find the player
}
wanderCounter -= Time.deltaTime;
if (wanderCounter <= 0)
{
wanderLocation = findWanderLocation();
wanderCounter = maxWanderCount;
}
seek(wanderLocation);
}
private Vector3 findWanderLocation()
{
RaycastHit hit;
float minX, maxX, minZ, maxZ, xPos, zPos;
if(Physics.Raycast(transform.position, transform.TransformDirection(transform.forward), out hit, rayDist))
{
Debug.Log("hit");
minX = transform.forward.x - 1;
maxX = transform.forward.x + 1;
minZ = transform.forward.z - wanderDist;
maxZ = transform.forward.z - (wanderDist + 1);
xPos = UnityEngine.Random.Range(minX, maxX);
zPos = UnityEngine.Random.Range(minZ, maxZ);
}
else
{
Debug.Log("Not hit");
minX = transform.forward.x - 1;
maxX = transform.forward.x + 1;
minZ = transform.forward.z + wanderDist;
maxZ = transform.forward.z + (wanderDist + 1);
xPos = UnityEngine.Random.Range(minX, maxX);
zPos = UnityEngine.Random.Range(minZ, maxZ);
}
return new Vector3(xPos, transform.position.y, zPos);
}
private void nonNavMesh()
{
if (!pathFound) // if a path hasn't been found
{
if (path.Count == 0)
{
zombieClosest = findClosestNodetoObject(this.gameObject); // find closest node to zombie
playerClosest = findClosestNodetoObject(player); // find closest node to player
path.Add(zombieClosest); // add the closest to path
if(zombieClosest.name == playerClosest.name)
{
behaviourState = aiState.CHASEPLAYER;
}
}
//potentialDirection is now a local list, cleans up code in memory.
List<GameObject> potentialDirection = new List<GameObject>();
potentialDirection = path[path.Count - 1].GetComponent<nodeScript>().attachedNodes; // gets attached nodes ** NOTE ** Getcomponents kill performance....
if (potentialDirection.Count == 1) // if only one attached node
{
path.Add(potentialDirection[0]); // add the node
}
else
{
List<GameObject> pickList = new List<GameObject>();
//pickList.Clear(); // clear the pick list
for (int i = 0; i < potentialDirection.Count; i++)
{
if (!path.Contains(potentialDirection[i])) // if the potential node is not on the path
{
pickList.Add(potentialDirection[i]); // add to pick list
}
}
GameObject addItem = null;
float distance = Mathf.Infinity;
for (int i = 0; i < pickList.Count; i++)
{
Vector3 dist = pickList[i].transform.position - playerClosest.transform.position; // subtract the position of the pick list form the closest node
float cDist = dist.sqrMagnitude; // calulate the square mag
if (cDist < distance) // if the new distance is shorter than the old distance
{
addItem = pickList[i]; // set current path add
distance = cDist; // se new shortest distance
}
}
path.Add(addItem); // add item to path
}
if (path[path.Count - 1] == playerClosest) // if the last item on the path is the closest node to player
{
pathFound = true; // set path to found
}
}
if (pathFound)
{
behaviourState = aiState.FOLLOWPATH; // change AI state
}
}
private void seek(Vector3 _target)
{
Vector3 targetPos = _target; // get target position
Vector3 dVelocity = Vector3.Normalize(targetPos - transform.position) * maxSpeed; // calulate desired velocity
Vector3 steer = dVelocity - rb.velocity; // calulate steering vector
rb.velocity += steer; // add steering vector to velocity
if(steer.sqrMagnitude > 0.0f)
{
transform.forward = Vector3.Normalize(new Vector3(steer.x, 0, steer.z));
}
if (behaviourState == aiState.FOLLOWPATH)
{
if (Vector3.Distance(transform.position, path[nodeIndex].transform.position) < nodeRadius)
{
if (nodeIndex >= path.Count - 1)
{
behaviourState = aiState.CHASEPLAYER;
}
else
{
nodeIndex++;
}
}
}
}
private GameObject findClosestNodetoObject(GameObject _object)
{
int closest = -1;
for (int i = 0; i < nodes.Length; i++)
{
if(closest == -1)
{
closest = i;
}
else
{
if(Vector3.Distance(_object.transform.position, nodes[i].transform.position) < Vector3.Distance(_object.transform.position, nodes[closest].transform.position))
{
closest = i;
}
}
}
return nodes[closest];
}
}