-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetectionController.cs
307 lines (253 loc) · 9.29 KB
/
DetectionController.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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectionController : MonoBehaviour
{
public bool isThisChild;
[HideInInspector]
public Transform detectionTransform;
public bool detectTargetAtTheLastPointOfPath;
[HideInInspector]
public float time_delayDetectionAtTheLastPoint;
[HideInInspector]
public float duration_detection;
public bool lightUp;
[HideInInspector]
public Light spotLight_front_ForDetection;
[HideInInspector]
public Light pointLight_front_ForDetection;
public GameObject Target;
public LayerMask layerMask_target;
public LayerMask layerMask_safeZone;
Collider[] objectsInDetectionRadius;
Vector3 dirToTarget;
RaycastHit hit;
bool hitObstacle;
public float detectionRadius;
[Range(0, 360)]
public float detectionRange;
private bool detect;
private bool detected;
private bool delayDetecting;
private bool isTargetinFront;
private bool Ended_durationOfdetection;
Animator animator_target;
Animator animator_this;
[HideInInspector]
public List<string> parameter_DetectedOnFalse;
[HideInInspector]
public int index_name_parameter;
MovementController script_movementController;
PathFollowingController script_PathFollowingController;
Vector3 position_this;
public HidingType hidingType;
private void OnDrawGizmos()
{
if (isThisChild && detectionTransform != null)
Gizmos.DrawWireSphere(detectionTransform.position, detectionRadius);
else
Gizmos.DrawWireSphere(transform.position, detectionRadius);
if(detect && hitObstacle)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, hit.point);
}
}
void Start()
{
if (isThisChild)
{
animator_this = detectionTransform.GetComponentInParent<Animator>();
script_movementController = detectionTransform.GetComponentInParent<MovementController>();
script_PathFollowingController = detectionTransform.GetComponentInParent<PathFollowingController>();
}
else
{
animator_this = GetComponent<Animator>();
script_movementController = GetComponent<MovementController>();
script_PathFollowingController = GetComponent<PathFollowingController>();
}
if (Target != null)
{
animator_target = Target.GetComponent<Animator>();
}
if (lightUp)
{
spotLight_front_ForDetection.enabled = false;
pointLight_front_ForDetection.enabled = false;
}
}
void FixedUpdate()
{
if (detectTargetAtTheLastPointOfPath) // if object is set to detect a target at the last poin of path
{
if (script_PathFollowingController.IsAtLastPoint() && !delayDetecting) // check if the object is at the last point of path
{
delayDetecting = true;
StartCoroutine(DetectTargetWithDelay(time_delayDetectionAtTheLastPoint));
}
else if (!script_PathFollowingController.IsAtLastPoint())
{
delayDetecting = false;
}
}
if (detect)
{
DetectTarget();
}
if (Target != null)
{
if (detected)
{
script_PathFollowingController.SetValueOf_followPath(false);
}
}
}
public IEnumerator DetectTargetWithDelay(float secs_delay)
{
detect = false;
detected = false;
yield return new WaitForSeconds(secs_delay);
StartCoroutine(DurationOfDetection(duration_detection));
}
IEnumerator DurationOfDetection(float secs_duration)
{
detect = true;
TurnOnLights(true);
yield return new WaitForSeconds(secs_duration);
TurnOnLights(false);
detect = false;
Ended_durationOfdetection = true;
}
void DetectTarget()
{
// on the Y position of the parent
if (isThisChild)
{
position_this = new Vector3(transform.position.x, detectionTransform.position.y, transform.position.z);
}
else
{
position_this = transform.position;
}
objectsInDetectionRadius = Physics.OverlapSphere(position_this, detectionRadius, layerMask_target);
for (int i = 0; i < objectsInDetectionRadius.Length; i++)
{
Transform target = objectsInDetectionRadius[i].transform;
Vector3 dirToTarget = (target.position - position_this).normalized;
// considering if "isThisChild = true", changing 'transform.forward' to parent's 'transform.forward'
if(Vector3.Angle(transform.forward, dirToTarget) < detectionRange / 2) // check if a target is in the detection range
{
// calculate the distance between this position and target's position
float dstToTarget = Vector3.Distance(position_this, target.position);
//if a target is not hiding on a certain hiding spot OR target is not in safe zone
HealthController targetHC = target.GetComponent<HealthController>();
// to prevent this object from detecting a target through grounds
hitObstacle = Physics.Raycast(transform.position, dirToTarget, out hit, dstToTarget, 1 << LayerMask.NameToLayer("Ground"));
// if the target is not hiding & not in the safe zone means that "target is detected."
if (IsNotHiding(target) && !targetHC.GetValue_isInSafeZone() && !hitObstacle && this.enabled)
{
detected = true;
// stop detecting because the moster's just detected the target. now it's time to attack
detect = false;
Ended_durationOfdetection = false;
StopAllCoroutines();
}
else
{
detected = false;
}
}
else
{
detected = false;
}
}
}
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.y;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
public void TurnOnLights(bool value)
{
if (lightUp)
{
spotLight_front_ForDetection.enabled = value;
pointLight_front_ForDetection.enabled = value;
}
}
public void SetValueOf_detect(bool value)
{
detect = value;
}
public bool GetValue_detected()
{
return detected;
}
public Vector3 GetValue_targetPosition()
{
return Target.transform.position;
}
public HealthController GetValue_TargetHealthController()
{
return Target.GetComponent<HealthController>();
}
public string GetTag_Target()
{
return Target.tag;
}
public bool GetValue_Ended_durationOfdetection()
{
return Ended_durationOfdetection;
}
public void SetValue_ended_durationOfDetection(bool value)
{
Ended_durationOfdetection = value;
}
public bool GetValue_detect()
{
return detect;
}
public List<string> Get_ParameterList()
{
List<string> parameterList = new List<string>(Target.GetComponent<Animator>().parameterCount);
for (int i = 0; i < Target.GetComponent<Animator>().parameterCount; i++)
{
if (Target.GetComponent<Animator>().GetParameter(i).type.ToString().Equals("Bool"))
{
parameterList.Add(Target.GetComponent<Animator>().GetParameter(i).name);
}
}
return parameterList;
}
public void SetValue_hidingType(HidingType value)
{
hidingType = value;
}
private bool IsNotHiding(Transform target)
{
HealthController targetHC = target.GetComponent<HealthController>();
// if a target is not hiding on a certain hiding spot
if (targetHC != null && !hidingType.Equals(targetHC.GetValue_hidingType()))
{
return true;
}
else
{
if (targetHC.GetValue_hidingType().Equals(HidingType.X_Axis)) // but if a target is hiding on X Axis,
{
// should check if a target is hiding on the Detector
if ((!targetHC.GetValue_onRight_HidingZone_X() && script_movementController.GetValue_facingRight()) ||
(targetHC.GetValue_onRight_HidingZone_X() && !script_movementController.GetValue_facingRight()))
{
return true;
}
}
return false;
}
}
}