-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCSeeker.cs
273 lines (209 loc) · 6.96 KB
/
RCSeeker.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
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// trying out wandering with raycasting and update
public class RCSeeker : MonoBehaviour
{
public float collisionDistance = 2;
public float speed = 1;
private float direction;
public GameObject Target;
public bool LockToCameraViewport;
private float viewportWidth;
private float viewportHeight;
private Bounds cameraBounds;
private string state;
private bool hasHitTarget;
private static bool DEBUG = true;
private static bool DEBUG_DRAW = true;
private List<Vector2> directions;
private float rotationRange = 5;
public int LayerToMask = 8;
// todo: store am array of directions
Vector2 origin;
// Use this for initialization
void Start ()
{
// start out seeking if the Target is within range
state = "seek";
hasHitTarget = false;
directions = new List<Vector2> ();
StartCoroutine (Seek ());
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name == "Player") {
hasHitTarget = true;
}
}
IEnumerator Seek ()
{
// check if there is something in the way of the target
while (!hasHitTarget && state=="seek") {
bool seek;
rotateToTarget ();
seek = seekCheck ();
if (seek) {
transform.Translate (Vector2.up * speed * Time.smoothDeltaTime);
} else {
state = "start-rotate";
}
yield return null;
}
}
IEnumerator AvoidMove ()
{
bool obstacle;
bool seek;
directions.Clear ();
while (state=="avoid") {
transform.Translate (Vector2.up * speed * Time.smoothDeltaTime);
// check if there is something in the way of the target
seek = seekCheck (); // checks for something in front of the Target game object
obstacle = obstacleCheck (); // checks for an obstacle directly in front
// inside here, if there is an obstacle, change state to "rotate"
if (obstacle) {
state = "start-rotate";
yield return null;
}
// if it's okay to see and there is no obstacle
if (seek && !obstacle) {
state = "seek";
StartCoroutine (Seek ());
}
yield return null;
}
}
RaycastHit2D hasObstacles (Vector2 direction, string colorString)
{
RaycastHit2D[] hits;
RaycastHit2D[] hitsLeft;
RaycastHit2D[] hitsRight;
Vector2 directionLeft;
Vector2 directionRight;
// move the origin of the Raycast so that it's outside of the collider
Color color;
// move the origin of the Raycast so that it's outside of the collider
switch (colorString) {
case "blue":
color = Color.cyan;
break;
case "red":
color = Color.red;
break;
case "yellow":
color = Color.yellow;
break;
default:
color = Color.black;
break;
}
if (DEBUG_DRAW) {
Debug.DrawRay (transform.position, direction * collisionDistance, color);
}
hits = Physics2D.RaycastAll (transform.position, direction, collisionDistance, 1 << LayerToMask);
Vector2 left = new Vector2 (-0.3F, 0);
Vector2 leftOrigin = new Vector2 (transform.position.x, transform.position.y) + left;
directionLeft = direction + left;
hitsLeft = Physics2D.RaycastAll (leftOrigin, directionLeft, collisionDistance, 1 << LayerToMask);
if (DEBUG_DRAW) {
Debug.DrawRay (leftOrigin, directionLeft * collisionDistance, color);
}
Vector2 right = new Vector2 (0.3F, 0);
Vector2 rightOrigin = new Vector2 (transform.position.x, transform.position.y) + right;
directionRight = direction + right;
hitsRight = Physics2D.RaycastAll (rightOrigin, directionRight, collisionDistance, 1 << LayerToMask);
if (DEBUG_DRAW) {
Debug.DrawRay (rightOrigin, directionRight * collisionDistance, color);
}
// is there a collision?
foreach (RaycastHit2D hit in hits) {
if (hit && hit.collider) {
return hit;
}
}
foreach (RaycastHit2D hit in hitsLeft) {
if (hit && hit.collider) {
return hit;
}
}
foreach (RaycastHit2D hit in hitsRight) {
if (hit && hit.collider) {
return hit;
}
}
RaycastHit2D falsey = new RaycastHit2D();
return falsey;
}
bool seekCheck ()
{
Vector2 direction = (Target.transform.position - transform.position).normalized;
bool hasObstacle = hasObstacles (direction, "blue");
return !hasObstacle;
}
bool obstacleCheck ()
{
Vector2 direction = transform.up; /* works for a top-down game, for a platformer try transform.forward */
bool hasObstacle = hasObstacles (direction, "yellow");
return hasObstacle;
}
void rotateToTarget ()
{
float zRotation = Mathf.Atan2 ((Target.transform.position.y - transform.position.y), (Target.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90;
transform.eulerAngles = new Vector3 (0, 0, zRotation);
}
// need optional last direction
IEnumerator rotateAround ()
{
RaycastHit2D obstacle;
while (state=="rotate") {
if (DEBUG) Debug.Log ("--state: rotate--");
float randomAngle = Random.Range (-rotationRange, rotationRange);
Vector3 directionChange = new Vector3 (randomAngle, 0, 0);
Vector3 direction;
Vector3 originalDirection;
originalDirection = Target.transform.position;
direction = originalDirection + directionChange;
obstacle = hasObstacles (direction, "yellow");
if (DEBUG) {
Debug.Log ("---obstacle?:---");
Debug.Log (!obstacle && !obstacle.collider);
}
if (!obstacle || !obstacle.collider) {
if (DEBUG) {
Debug.Log ("---no obstacle, moving:---");
}
Vector3 directionPoint = transform.position + direction.normalized;
Vector3 vectorToTarget = directionPoint - transform.position;
float zRotation = Mathf.Atan2 (vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg - 90;
transform.eulerAngles = new Vector3 (0, 0, zRotation);
// if there is no obstacle, then actually rotate in that direction
state = "start-move";
yield return null;
} else {
// usually when the seeker is right against the object, bump it out
transform.Translate (obstacle.normal * speed * Time.smoothDeltaTime);
yield return null;
}
}
}
// Update is called once per frame
void Update ()
{
switch (state) {
case "start-move":
state = "avoid";
StartCoroutine (AvoidMove ());
break;
case "start-rotate":
state = "rotate";
StartCoroutine (rotateAround ());
break;
default:
break;
}
}
}