-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCWander.cs
187 lines (132 loc) · 4.38 KB
/
RCWander.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
// ------------------------------------------------------------------------------
// <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 RCWander : MonoBehaviour {
public float collisionDistance = 2;
public float speed = 1;
private float direction;
public bool LockToCameraViewport;
private float viewportWidth;
private float viewportHeight;
private Bounds cameraBounds;
private string state;
private static bool DEBUG = false; // to turn debug messages on an off
private static bool DEBUG_DRAW = false; // to turn Debug lines on and off
public int LayerToMask = 8; // make sure your player isn't on this list!
public int straightAhead = 10; // how much to go in a given direction before turning
private int count;
// todo: store am array of directions
Vector2 origin;
// Use this for initialization
void Start () {
// start out seeking if the Target is within range
state = "wander";
count = straightAhead;
StartCoroutine( Wander () );
}
IEnumerator Wander() {
// check if there is something in the way of the target
while (state=="wander" && count > 0) {
bool obstacles;
Vector2 direction = transform.up;
if (DEBUG) Debug.Log (direction);
obstacles = hasObstacles(direction, "blue");
if (DEBUG) Debug.Log ("wander: has obstacle");
if (DEBUG) Debug.Log (obstacles);
if (!obstacles) {
transform.Translate(Vector2.up * speed * Time.smoothDeltaTime);
} else {
if (DEBUG) Debug.Log ("wander: start rotating");
state="start-rotate";
yield return null;
}
count--;
if (count == 0) state = "start-rotate";
yield return null;
}
}
bool 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;
}
bool hasObstacle = false;
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) {
hasObstacle = true;
}
}
foreach(RaycastHit2D hit in hitsLeft) {
if (hit && hit.collider) {
hasObstacle = true;
}
}
foreach(RaycastHit2D hit in hitsRight) {
if (hit && hit.collider) {
hasObstacle = true;
}
}
return hasObstacle;
}
// need optional last direction
IEnumerator rotatePlayer() {
while(state=="rotate") {
if (DEBUG) Debug.Log ("wander: rotating player");
if (DEBUG_DRAW) Debug.DrawRay(transform.position, transform.right, Color.green);
if (DEBUG) Debug.Log ("rotating");
transform.rotation = Random.rotation;
/* set rotation to only be on the z-axis for 2D */
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
state = "wander";
StartCoroutine( Wander () );
yield return null;
}
}
// Update is called once per frame
void Update () {
switch(state) {
case "start-rotate":
state="rotate";
count = straightAhead;
StartCoroutine( rotatePlayer() );
break;
default:
break;
}
}
}