-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPlayerMovement.cs
518 lines (441 loc) · 14.8 KB
/
PlayerMovement.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//Ground
float groundSpeed = 4f;
float runSpeed = 6f;
float grAccel = 20f;
//Air
float airSpeed = 3f;
float airAccel = 20f;
//Jump
float jumpUpSpeed = 9.2f;
float dashSpeed = 6f;
//Wall
float wallSpeed = 10f;
float wallClimbSpeed = 4f;
float wallAccel = 20f;
float wallRunTime = 3f;
float wallStickiness = 20f;
float wallStickDistance = 1f;
float wallFloorBarrier = 40f;
float wallBanTime = 4f;
Vector3 bannedGroundNormal;
//Cooldowns
bool canJump = true;
bool canDJump = true;
float wallBan = 0f;
float wrTimer = 0f;
float wallStickTimer = 0f;
//States
bool running;
bool jump;
bool crouched;
bool grounded;
Collider ground;
Vector3 groundNormal = Vector3.up;
CapsuleCollider col;
enum Mode
{
Walking,
Flying,
Wallruning
}
Mode mode = Mode.Flying;
CameraController camCon;
Rigidbody rb;
Vector3 dir = Vector3.zero;
void Start()
{
rb = GetComponent<Rigidbody>();
camCon = GetComponentInChildren<CameraController>();
col = GetComponent<CapsuleCollider>();
}
void OnGUI()
{
GUILayout.Label("Spid: " + new Vector3(rb.velocity.x, 0, rb.velocity.z).magnitude);
GUILayout.Label("SpidUp: " + rb.velocity.y);
}
void Update()
{
col.material.dynamicFriction = 0f;
dir = Direction();
running = (Input.GetKey(KeyCode.LeftShift) && Input.GetAxisRaw("Vertical") > 0.9);
crouched = (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.C));
if (Input.GetKeyDown(KeyCode.Space))
{
jump = true;
}
//Special use
//if (Input.GetKeyDown(KeyCode.T)) transform.position = new Vector3(0f, 30f, 0f);
//if (Input.GetKeyDown(KeyCode.X)) rb.velocity = new Vector3(rb.velocity.x, 40f, rb.velocity.z);
//if (Input.GetKeyDown(KeyCode.V)) rb.AddForce(dir * 20f, ForceMode.VelocityChange);
}
void FixedUpdate()
{
if (crouched)
{
col.height = Mathf.Max(0.6f, col.height - Time.deltaTime * 10f);
}
else
{
col.height = Mathf.Min(1.8f, col.height + Time.deltaTime * 10f);
}
if (wallStickTimer == 0f && wallBan > 0f)
{
bannedGroundNormal = groundNormal;
}
else
{
bannedGroundNormal = Vector3.zero;
}
wallStickTimer = Mathf.Max(wallStickTimer - Time.deltaTime, 0f);
wallBan = Mathf.Max(wallBan - Time.deltaTime, 0f);
switch (mode)
{
case Mode.Wallruning:
camCon.SetTilt(WallrunCameraAngle());
Wallrun(dir, wallSpeed, wallClimbSpeed, wallAccel);
if (ground.tag != "InfiniteWallrun") wrTimer = Mathf.Max(wrTimer - Time.deltaTime, 0f);
break;
case Mode.Walking:
camCon.SetTilt(0);
Walk(dir, running ? runSpeed : groundSpeed, grAccel);
break;
case Mode.Flying:
camCon.SetTilt(0);
AirMove(dir, airSpeed, airAccel);
break;
}
jump = false;
}
private Vector3 Direction()
{
float hAxis = Input.GetAxisRaw("Horizontal");
float vAxis = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(hAxis, 0, vAxis);
return rb.transform.TransformDirection(direction);
}
#region Collisions
void OnCollisionStay(Collision collision)
{
if (collision.contactCount > 0)
{
float angle;
foreach (ContactPoint contact in collision.contacts)
{
angle = Vector3.Angle(contact.normal, Vector3.up);
if (angle < wallFloorBarrier)
{
EnterWalking();
grounded = true;
groundNormal = contact.normal;
ground = contact.otherCollider;
return;
}
}
if (VectorToGround().magnitude > 0.2f)
{
grounded = false;
}
if (grounded == false)
{
foreach (ContactPoint contact in collision.contacts)
{
if (contact.otherCollider.tag != "NoWallrun" && contact.otherCollider.tag != "Player" && mode != Mode.Walking)
{
angle = Vector3.Angle(contact.normal, Vector3.up);
if (angle > wallFloorBarrier && angle < 120f)
{
grounded = true;
groundNormal = contact.normal;
ground = contact.otherCollider;
EnterWallrun();
return;
}
}
}
}
}
}
void OnCollisionExit(Collision collision)
{
if (collision.contactCount == 0)
{
EnterFlying();
}
}
#endregion
#region Entering States
void EnterWalking()
{
if (mode != Mode.Walking && canJump)
{
if (mode == Mode.Flying && crouched)
{
rb.AddForce(-rb.velocity.normalized, ForceMode.VelocityChange);
}
if (rb.velocity.y < -1.2f)
{
//camCon.Punch(new Vector2(0, -3f));
}
//StartCoroutine(bHopCoroutine(bhopLeniency));
gameObject.SendMessage("OnStartWalking");
mode = Mode.Walking;
}
}
void EnterFlying(bool wishFly = false)
{
grounded = false;
if (mode == Mode.Wallruning && VectorToWall().magnitude < wallStickDistance && !wishFly)
{
return;
}
else if (mode != Mode.Flying)
{
wallBan = wallBanTime;
canDJump = true;
mode = Mode.Flying;
}
}
void EnterWallrun()
{
if (mode != Mode.Wallruning)
{
if (VectorToGround().magnitude > 0.2f && CanRunOnThisWall(bannedGroundNormal) && wallStickTimer == 0f)
{
gameObject.SendMessage("OnStartWallrunning");
wrTimer = wallRunTime;
canDJump = true;
mode = Mode.Wallruning;
}
else
{
EnterFlying(true);
}
}
}
#endregion
#region Movement Types
void Walk(Vector3 wishDir, float maxSpeed, float acceleration)
{
if (jump && canJump)
{
gameObject.SendMessage("OnJump");
Jump();
}
else
{
//if (crouched) acceleration = 0.5f;
wishDir = wishDir.normalized;
Vector3 spid = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (spid.magnitude > maxSpeed) acceleration *= spid.magnitude / maxSpeed;
Vector3 direction = wishDir * maxSpeed - spid;
if (direction.magnitude < 0.5f)
{
acceleration *= direction.magnitude / 0.5f;
}
direction = direction.normalized * acceleration;
float magn = direction.magnitude;
direction = direction.normalized;
direction *= magn;
Vector3 slopeCorrection = groundNormal * Physics.gravity.y / groundNormal.y;
slopeCorrection.y = 0f;
//if(!crouched)
direction += slopeCorrection;
rb.AddForce(direction, ForceMode.Acceleration);
}
}
void AirMove(Vector3 wishDir, float maxSpeed, float acceleration)
{
if (jump && !crouched)
{
gameObject.SendMessage("OnDoubleJump");
DoubleJump(wishDir);
}
if (crouched && rb.velocity.y > -10 && Input.GetKey(KeyCode.Space))
{
rb.AddForce(Vector3.down * 20f, ForceMode.Acceleration);
}
float projVel = Vector3.Dot(new Vector3(rb.velocity.x, 0f, rb.velocity.z), wishDir); // Vector projection of Current velocity onto accelDir.
float accelVel = acceleration * Time.deltaTime; // Accelerated velocity in direction of movment
// If necessary, truncate the accelerated velocity so the vector projection does not exceed max_velocity
if (projVel + accelVel > maxSpeed)
accelVel = Mathf.Max(0f, maxSpeed - projVel);
rb.AddForce(wishDir.normalized * accelVel, ForceMode.VelocityChange);
}
void Wallrun(Vector3 wishDir, float maxSpeed, float climbSpeed, float acceleration)
{
if (jump)
{
//Vertical
float upForce = Mathf.Clamp(jumpUpSpeed - rb.velocity.y, 0, Mathf.Infinity);
rb.AddForce(new Vector3(0, upForce, 0), ForceMode.VelocityChange);
//Horizontal
Vector3 jumpOffWall = groundNormal.normalized;
jumpOffWall *= dashSpeed;
jumpOffWall.y = 0;
rb.AddForce(jumpOffWall, ForceMode.VelocityChange);
wrTimer = 0f;
EnterFlying(true);
}
else if (wrTimer == 0f || crouched)
{
rb.AddForce(groundNormal * 3f, ForceMode.VelocityChange);
EnterFlying(true);
}
else
{
//Horizontal
Vector3 distance = VectorToWall();
wishDir = RotateToPlane(wishDir, -distance.normalized);
wishDir *= maxSpeed;
wishDir.y = Mathf.Clamp(wishDir.y, -climbSpeed, climbSpeed);
Vector3 wallrunForce = wishDir - rb.velocity;
if (wallrunForce.magnitude > 0.2f) wallrunForce = wallrunForce.normalized * acceleration;
//Vertical
if (rb.velocity.y < 0f && wishDir.y > 0f) wallrunForce.y = 2f * acceleration;
//Anti-gravity force
Vector3 antiGravityForce = -Physics.gravity;
if (wrTimer < 0.33 * wallRunTime)
{
antiGravityForce *= wrTimer / wallRunTime;
wallrunForce += (Physics.gravity + antiGravityForce);
}
//Forces
rb.AddForce(wallrunForce, ForceMode.Acceleration);
rb.AddForce(antiGravityForce, ForceMode.Acceleration);
if (distance.magnitude > wallStickDistance) distance = Vector3.zero;
rb.AddForce(distance * wallStickiness, ForceMode.Acceleration);
}
if (!grounded)
{
wallStickTimer = 0.2f;
EnterFlying();
}
}
void Jump()
{
if (mode == Mode.Walking && canJump)
{
float upForce = Mathf.Clamp(jumpUpSpeed - rb.velocity.y, 0, Mathf.Infinity);
rb.AddForce(new Vector3(0, upForce, 0), ForceMode.VelocityChange);
StartCoroutine(jumpCooldownCoroutine(0.2f));
EnterFlying(true);
}
}
void DoubleJump(Vector3 wishDir)
{
if (canDJump)
{
//Vertical
float upForce = Mathf.Clamp(jumpUpSpeed - rb.velocity.y, 0, Mathf.Infinity);
rb.AddForce(new Vector3(0, upForce, 0), ForceMode.VelocityChange);
//Horizontal
if (wishDir != Vector3.zero)
{
Vector3 horSpid = new Vector3(rb.velocity.x, 0, rb.velocity.z);
Vector3 newSpid = wishDir.normalized;
float newSpidMagnitude = dashSpeed;
if (horSpid.magnitude > dashSpeed)
{
float dot = Vector3.Dot(wishDir.normalized, horSpid.normalized);
if (dot > 0)
{
newSpidMagnitude = dashSpeed + (horSpid.magnitude - dashSpeed) * dot;
}
else
{
newSpidMagnitude = Mathf.Clamp(dashSpeed * (1 + dot), dashSpeed * (dashSpeed / horSpid.magnitude), dashSpeed);
}
}
newSpid *= newSpidMagnitude;
rb.AddForce(newSpid - horSpid, ForceMode.VelocityChange);
}
canDJump = false;
}
}
#endregion
#region MathGenious
Vector2 ClampedAdditionVector(Vector2 a, Vector2 b)
{
float k, x, y;
k = Mathf.Sqrt(Mathf.Pow(a.x, 2) + Mathf.Pow(a.y, 2)) / Mathf.Sqrt(Mathf.Pow(a.x + b.x, 2) + Mathf.Pow(a.y + b.y, 2));
x = k * (a.x + b.x) - a.x;
y = k * (a.y + b.y) - a.y;
return new Vector2(x, y);
}
Vector3 RotateToPlane(Vector3 vect, Vector3 normal)
{
Vector3 rotDir = Vector3.ProjectOnPlane(normal, Vector3.up);
Quaternion rotation = Quaternion.AngleAxis(-90f, Vector3.up);
rotDir = rotation * rotDir;
float angle = -Vector3.Angle(Vector3.up, normal);
rotation = Quaternion.AngleAxis(angle, rotDir);
vect = rotation * vect;
return vect;
}
float WallrunCameraAngle()
{
Vector3 rotDir = Vector3.ProjectOnPlane(groundNormal, Vector3.up);
Quaternion rotation = Quaternion.AngleAxis(-90f, Vector3.up);
rotDir = rotation * rotDir;
float angle = Vector3.SignedAngle(Vector3.up, groundNormal, Quaternion.AngleAxis(90f, rotDir) * groundNormal);
angle -= 90;
angle /= 180;
Vector3 playerDir = transform.forward;
Vector3 normal = new Vector3(groundNormal.x, 0, groundNormal.z);
return Vector3.Cross(playerDir, normal).y * angle;
}
bool CanRunOnThisWall(Vector3 normal)
{
if (Vector3.Angle(normal, groundNormal) > 10 || wallBan == 0f)
{
return true;
}
else
{
return false;
}
}
Vector3 VectorToWall()
{
Vector3 direction;
Vector3 position = transform.position + Vector3.up * col.height / 2f;
RaycastHit hit;
if (Physics.Raycast(position, -groundNormal, out hit, wallStickDistance) && Vector3.Angle(groundNormal, hit.normal) < 70)
{
groundNormal = hit.normal;
direction = hit.point - position;
return direction;
}
else
{
return Vector3.positiveInfinity;
}
}
Vector3 VectorToGround()
{
Vector3 position = transform.position;
RaycastHit hit;
if (Physics.Raycast(position, Vector3.down, out hit, wallStickDistance))
{
return hit.point - position;
}
else
{
return Vector3.positiveInfinity;
}
}
#endregion
#region Coroutines
IEnumerator jumpCooldownCoroutine(float time)
{
canJump = false;
yield return new WaitForSeconds(time);
canJump = true;
}
#endregion
}