-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollisionInformation.cs
335 lines (264 loc) · 10.3 KB
/
CollisionInformation.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionInformation : MonoBehaviour
{
[Range(2, 50)]
public int up_ray_count = 2;
[Range(2, 50)]
public int down_ray_count = 2;
[Range(2, 50)]
public int left_ray_count = 2;
[Range(2, 50)]
public int right_ray_count = 2;
public float max_slope_angle = 60;
[Tooltip("Units between the ray's starting points, and when it actually starts counting distance." +
"Setting this at 0 is not recommended because if the raycast starts inside a collider, then the raycast" +
"won't be able to detect collision. Do not set beyond the player's height or width either as weird behavior will" +
"occur")]
public float raycast_breathing_room = .2f;
public float height = 1.98f;
public float width = 1;
private float up_ray_spacing = 0;
private float down_ray_spacing = 0;
private float left_ray_spacing = 0;
private float right_ray_spacing = 0;
private float[] distances_up;
private float[] distances_down;
private float[] distances_left;
private float[] distances_right;
private float shortest_distance_up = 0;
private float shortest_distance_down = 0;
private float shortest_distance_left = 0;
private float shortest_distance_right = 0;
private bool is_grounded = false;
private bool collision_left = false;
private bool collision_right = false;
private bool collision_up = false;
private bool is_climbing_slope = false;
private bool is_decending_slope = false;
private float slope_up_right_angle = 0f;
private float slope_up_left_angle = 0f;
private float slope_down_left_angle = 0f;
private float slope_down_right_angle = 0f;
private float last_slope_angle;
private Vector2 slope_normal_perpandicular;
private const float MAX_DISTANCE = 10;
private float hit_left_debug = 0;
private float hit_right_debug = 0;
Vector2 top_left_corner;
Vector2 bottom_right_corner;
// Use this for initialization
void Start()
{
distances_up = new float[up_ray_count];
distances_down = new float[down_ray_count];
distances_left = new float[left_ray_count];
distances_right = new float[right_ray_count];
up_ray_spacing = width / (up_ray_count - 1);
down_ray_spacing = width / (down_ray_count - 1);
left_ray_spacing = height / (left_ray_count - 1);
right_ray_spacing = height / (right_ray_count - 1);
}
public void updateDistances()
{
top_left_corner = new Vector2(transform.position.x - (width / 2), transform.position.y + (height / 2));
bottom_right_corner = new Vector2(transform.position.x + (width / 2), transform.position.y - (height / 2));
shortest_distance_up = MAX_DISTANCE;
shortest_distance_down = MAX_DISTANCE;
shortest_distance_left = MAX_DISTANCE;
shortest_distance_right = MAX_DISTANCE;
//transform.position + (spacing_direction * starting_distance)
shortest_distance_up = getDataFromRayDirection(top_left_corner, Vector2.up, Vector2.right, up_ray_spacing, ref distances_up);
shortest_distance_down = getDataFromRayDirection(bottom_right_corner, Vector3.down, Vector2.left, down_ray_spacing, ref distances_down);
shortest_distance_left = getDataFromRayDirection(top_left_corner, Vector3.left, Vector2.down, left_ray_spacing, ref distances_left);
shortest_distance_right = getDataFromRayDirection(bottom_right_corner, Vector3.right, Vector3.up, right_ray_spacing, ref distances_right);
updateCollisions();
}
public void updateSlopes(Vector2 velocity)
{
updateSideSlopes();
updateDownSlopes(velocity.x);
}
public float getShortestDistanceUp()
{
return shortest_distance_up;
}
public float getShortestDistanceDown()
{
return shortest_distance_down;
}
public float getShortestDistanceLeft()
{
return shortest_distance_left;
}
public float getShortestDistanceRight()
{
return shortest_distance_right;
}
public bool headCollision()
{
return collision_up;
}
public bool leftCollision()
{
return collision_left;
}
public bool rightCollision()
{
return collision_right;
}
public bool isGrounded()
{
return is_grounded;
}
public float getHeight()
{
return height;
}
public float getWidth()
{
return width;
}
private void updateCollisions()
{
if (shortest_distance_down < .02f && shortest_distance_down >= -.02f)
is_grounded = true;
else
is_grounded = false;
if (shortest_distance_right < .02f && shortest_distance_right >= -.02f)
collision_right = true;
else
collision_right = false;
if (shortest_distance_left < .02f && shortest_distance_left >= -.02f)
collision_left = true;
else
collision_left = false;
if (shortest_distance_up < .02f && shortest_distance_up >= -.02f)
collision_up = true;
else
collision_up = false;
}
public bool isClimbingSlope()
{
return is_climbing_slope;
}
public bool isDecendingSlope()
{
return is_decending_slope;
}
public float getUpRightAngle()
{
return slope_up_right_angle;
}
public float getUpLeftAngle()
{
return slope_up_left_angle;
}
public float getDownLeftAngle()
{
return slope_down_left_angle;
}
public float getDownRightAngle()
{
return slope_down_right_angle;
}
private void updateSideSlopes()
{
RaycastHit2D hit_right = Physics2D.Raycast(bottom_right_corner, Vector2.right);
RaycastHit2D hit_left = Physics2D.Raycast(bottom_right_corner + (width * Vector2.left), Vector2.left);
//Draw rays for debugging purposes.
Debug.DrawRay(bottom_right_corner, Vector2.right * 2, Color.magenta);
Debug.DrawRay(bottom_right_corner + (width * Vector2.left), Vector3.left * 2, Color.magenta);
slope_up_right_angle = 0f;
slope_up_left_angle = 0.0f;
hit_left_debug = hit_left.distance;
hit_right_debug = hit_right.distance;
if (hit_right.collider != null
&& hit_right.distance <= .075f)
{
slope_up_right_angle = Vector2.Angle(hit_right.normal, Vector2.up);
if(slope_up_right_angle <= max_slope_angle)
{
is_climbing_slope = true;
collision_right = false;
}
return;
}
if (hit_left.collider != null
&& hit_left.distance <= .075f)
{
slope_up_left_angle = Vector2.Angle(hit_left.normal, Vector2.up);
if (slope_up_left_angle <= max_slope_angle)
{
is_climbing_slope = true;
collision_left = false;
}
return;
}
is_climbing_slope = false;
}
public void updateDownSlopes(float velocity_x)
{
//Starting from the center of the player, go down half the player's height, then go right half the width (bottom right corner of the player).
Vector2 origin_right = transform.position + (Vector3.down * height / 2f) + (Vector3.right * width / 2);
// Bottom Left corner of the player
Vector2 origin_left = transform.position + (Vector3.down * height / 2f) + (Vector3.left * width / 2);
RaycastHit2D hit_right = Physics2D.Raycast(origin_right, Vector2.down);
RaycastHit2D hit_left = Physics2D.Raycast(origin_left, Vector2.down);
Debug.DrawRay(origin_right, Vector3.down, Color.magenta);
Debug.DrawRay(origin_left, Vector3.down, Color.magenta);
bool temp_is_decending = false;
if (hit_right.collider != null)
{
float temp_angle = Vector2.Angle(hit_right.normal, Vector2.up);
if ((temp_angle != 0 && temp_angle <= max_slope_angle)
&& hit_right.distance - .1 <= Mathf.Tan(temp_angle * Mathf.Deg2Rad) * Mathf.Abs(velocity_x))
{
slope_down_right_angle = temp_angle;
temp_is_decending = true;
is_decending_slope = true;
is_grounded = true;
}
}
if (hit_left.collider != null)
{
float temp_angle = Vector2.Angle(hit_left.normal, Vector2.up);
if ((temp_angle != 0 && temp_angle <= max_slope_angle)
&& hit_left.distance - .1 <= Mathf.Tan(temp_angle * Mathf.Deg2Rad) * Mathf.Abs(velocity_x))
{
slope_down_left_angle = temp_angle;
temp_is_decending = true;
is_decending_slope = true;
is_grounded = true;
}
}
if(!temp_is_decending)
{
slope_down_left_angle = 0;
slope_down_right_angle = 0;
is_decending_slope = false;
}
}
/**
* Draws a number of rays an equal amount of spacing from each other. These rays will all be within the height/width of the player.
* Each ray will record the distance between the starting point and first collision. This function will return the shortest distance.
*/
public float getDataFromRayDirection(Vector2 starting_point, Vector2 direction_of_ray, Vector2 direction_of_spacing, float spacing_distance, ref float[] distance_array)
{
float shortest_distance = MAX_DISTANCE;
for (int i = 0; i < distance_array.Length; i++)
{
Vector2 origin = starting_point + (direction_of_spacing * (spacing_distance * i));
RaycastHit2D hit = Physics2D.Raycast(origin, direction_of_ray);
Debug.DrawRay(origin, direction_of_ray);
if (hit.collider != null)
{
distance_array[i] = hit.distance - raycast_breathing_room; //Record all distances for debugging purposes
if (hit.distance - raycast_breathing_room < shortest_distance) // Record which is the shortest distance
shortest_distance = hit.distance - raycast_breathing_room;
}
}
return shortest_distance;
}
}