-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovement.cs
943 lines (837 loc) · 37.1 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
// based on Unity's FirstPersonController & ThirdPersonController scripts
using UnityEngine;
using StandardAssets.Characters.Physics;
using Random = UnityEngine.Random;
// MoveState as byte for minimal bandwidth (otherwise it's int by default)
// note: distinction between WALKING and RUNNING in case we need to know the
// difference somewhere (e.g. for endurance recovery)
public enum MoveState : byte {IDLE, WALKING, RUNNING, CROUCHING, CRAWLING, AIRBORNE, CLIMBING, SWIMMING, MOUNTED, MOUNTED_AIRBORNE, MOUNTED_SLIDING, MOUNTED_SWIMMING, DEAD}
[RequireComponent(typeof(OpenCharacterController))]
[RequireComponent(typeof(AudioSource))]
public class PlayerMovement : MonoBehaviour
{
// components to be assigned in inspector
[Header("Components")]
public Animator animator;
public Health health;
public OpenCharacterController controller;
public AudioSource feetAudio;
public Combat combat;
public PlayerLook look;
public Endurance endurance;
public PlayerMountUsage mountUsage;
new public Collider collider;
new Camera camera;
[Header("State")]
public MoveState state = MoveState.IDLE;
[HideInInspector] public Vector3 moveDir;
// it's useful to have both strafe movement (WASD) and rotations (QE)
// => like in WoW, it more fun to play this way.
[Header("Rotation")]
public float rotationSpeed = 150;
[Header("Walking")]
public float walkSpeed = 5;
public float walkAcceleration = 15; // set to maxint for instant speed
public float walkDeceleration = 20; // feels best if higher than acceleration
[Header("Running")]
public float runSpeed = 8;
[Range(0f, 1f)] public float runStepLength = 0.7f;
public float runStepInterval = 3;
public float runCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
public KeyCode runKey = KeyCode.LeftShift;
float stepCycle;
float nextStep;
[Header("Crouching")]
public float crouchSpeed = 1.5f;
public KeyCode crouchKey = KeyCode.C;
public float crouchAcceleration = 5; // set to maxint for instant speed
public float crouchDeceleration = 10; // feels best if higher than acceleration
[Header("Crawling")]
public float crawlSpeed = 1;
public KeyCode crawlKey = KeyCode.V;
public float crawlAcceleration = 5; // set to maxint for instant speed
public float crawlDeceleration = 10; // feels best if higher than acceleration
[Header("Swimming")]
public float swimSpeed = 4;
public float swimAcceleration = 15; // set to maxint for instant speed
public float swimDeceleration = 20; // feels best if higher than acceleration
public float swimSurfaceOffset = 0.25f;
Collider waterCollider;
bool inWater => waterCollider != null; // standing in water / touching it?
bool underWater; // deep enough in water so we need to swim?
[Range(0, 1)] public float underwaterThreshold = 0.7f; // percent of body that need to be underwater to start swimming
public LayerMask canStandInWaterCheckLayers = Physics.DefaultRaycastLayers; // set this to everything except water layer
[Header("Jumping")]
public float jumpSpeed = 7;
[HideInInspector] public float jumpLeg;
[Header("Falling")]
public float airborneAcceleration = 15; // set to maxint for instant speed
public float airborneDeceleration = 20; // feels best if higher than acceleration
public float fallMinimumMagnitude = 9; // walking down steps shouldn't count as falling and play no falling sound.
public float fallDamageMinimumMagnitude = 13;
public float fallDamageMultiplier = 2;
Vector3 lastFall;
[Header("Climbing")]
public float climbSpeed = 3;
public float climbAcceleration = 5; // set to maxint for instant speed
public float climbDeceleration = 10; // feels best if higher than acceleration
Collider ladderCollider;
[Header("Mounted")]
public float mountedRotationSpeed = 100;
public float mountedAcceleration = 15; // set to maxint for instant speed
public float mountedDeceleration = 20; // feels best if higher than acceleration
[Header("Physics")]
public float gravityMultiplier = 2;
// we need to remember the last accelerated xz speed without gravity etc.
// (using moveDir.xz.magnitude doesn't work well with mounted movement)
float horizontalSpeed;
// helper property to check grounded with some tolerance. technically we
// aren't grounded when walking down steps, but this way we factor in a
// minimum fall magnitude. useful for more tolerant jumping etc.
// (= while grounded or while velocity not smaller than min fall yet)
public bool isGroundedWithinTolerance =>
controller.isGrounded || controller.velocity.y > -fallMinimumMagnitude;
[Header("Sounds")]
public AudioClip[] footstepSounds; // an array of footstep sounds that will be randomly selected from.
public AudioClip jumpSound; // the sound played when character leaves the ground.
public AudioClip landSound; // the sound played when character touches back on ground.
// input directions ////////////////////////////////////////////////////////
Vector2 GetInputDirection()
{
// get input direction while alive and while not typing in chat
// (otherwise 0 so we keep falling even if we die while jumping etc.)
float horizontal = 0;
float vertical = 0;
if (!UIUtils.AnyInputActive())
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
}
return new Vector2(horizontal, vertical).normalized;
}
Vector3 GetDesiredDirection(Vector2 inputDir)
{
// always move along the camera forward as it is the direction that is being aimed at
return transform.forward * inputDir.y + transform.right * inputDir.x;
}
Vector3 GetDesiredDirectionOnGround(Vector3 desiredDir)
{
// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, controller.radius, Vector3.down, out hitInfo,
controller.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
return Vector3.ProjectOnPlane(desiredDir, hitInfo.normal).normalized;
}
return desiredDir;
}
// movement state machine //////////////////////////////////////////////////
bool EventDied()
{
return health.current == 0;
}
bool EventJumpRequested()
{
// only while grounded, so jump key while jumping doesn't start a new
// jump immediately after landing
// => and not while sliding, otherwise we could climb slides by jumping
// => grounded check uses min fall tolerance so we can actually still
// jump when walking down steps.
return isGroundedWithinTolerance &&
!controller.isSlidingDownSlope &&
!UIUtils.AnyInputActive() && Input.GetButtonDown("Jump");
}
bool EventCrouchToggle()
{
return !UIUtils.AnyInputActive() && Input.GetKeyDown(crouchKey);
}
bool EventCrawlToggle()
{
return !UIUtils.AnyInputActive() && Input.GetKeyDown(crawlKey);
}
bool EventFalling()
{
// use minimum fall magnitude so walking down steps isn't detected as
// falling! otherwise walking down steps would show the fall animation
// and play the landing sound.
return !isGroundedWithinTolerance;
}
bool EventLanded()
{
return controller.isGrounded;
}
bool EventMounted()
{
return mountUsage.IsMounted();
}
bool EventDismounted()
{
return !mountUsage.IsMounted();
}
bool EventUnderWater()
{
// we can't really make it player position dependent, because he might
// swim to the surface at which point it might be detected as standing
// in water but not being under water, etc.
if (inWater) // in water and valid water collider?
{
// raycasting from water to the bottom at the position of the player
// seems like a very precise solution
Vector3 origin = new Vector3(transform.position.x,
waterCollider.bounds.max.y,
transform.position.z);
float distance = collider.bounds.size.y * underwaterThreshold;
Debug.DrawLine(origin, origin + Vector3.down * distance, Color.cyan);
// we are underwater if the raycast doesn't hit anything
RaycastHit hit;
return !Utils.RaycastWithout(origin, Vector3.down, out hit, distance, gameObject, canStandInWaterCheckLayers);
}
return false;
}
bool EventLadderExit()
{
// OnTriggerExit isn't good enough to detect ladder exits because we
// shouldnt exit as soon as our head sticks out of the ladder collider.
// only if we fully left it. so check this manually here:
return ladderCollider != null &&
!ladderCollider.bounds.Intersects(collider.bounds);
}
// helper function to apply gravity based on previous Y direction
float ApplyGravity(float moveDirY)
{
// apply full gravity while falling
if (!controller.isGrounded)
// gravity needs to be * Time.deltaTime even though we multiply the
// final controller.Move * Time.deltaTime too, because the unit is
// 9.81m/s²
return moveDirY + Physics.gravity.y * gravityMultiplier * Time.deltaTime;
// if grounded then apply no force. the new OpenCharacterController
// doesn't need a ground stick force. it would only make the character
// slide on all uneven surfaces.
return 0;
}
// helper function to get move or walk speed depending on key press & endurance
float GetWalkOrRunSpeed()
{
bool runRequested = !UIUtils.AnyInputActive() && Input.GetKey(runKey);
return runRequested && endurance.current > 0 ? runSpeed : walkSpeed;
}
void ApplyFallDamage()
{
// measure only the Y direction. we don't want to take fall damage
// if we jump forward into a wall because xz is high.
float fallMagnitude = Mathf.Abs(lastFall.y);
if(fallMagnitude >= fallDamageMinimumMagnitude)
{
int damage = Mathf.RoundToInt(fallMagnitude * fallDamageMultiplier);
health.current -= damage;
combat.ShowDamageEffect(damage, transform.position, -lastFall);
}
}
// acceleration can be different when accelerating/decelerating
float AccelerateSpeed(Vector2 inputDir, float currentSpeed, float targetSpeed, float acceleration)
{
// desired speed is between 'speed' and '0'
float desiredSpeed = inputDir.magnitude * targetSpeed;
// accelerate speed
return Mathf.MoveTowards(currentSpeed, desiredSpeed, acceleration * Time.deltaTime);
}
MoveState UpdateIDLE(Vector2 inputDir, Vector3 desiredDir)
{
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// decelerate from last move (e.g. from jump)
// (moveDir.xz can be set to 0 to have an interruption when landing)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, 0, walkDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventFalling())
{
return MoveState.AIRBORNE;
}
else if (EventJumpRequested())
{
// start the jump movement into Y dir, go to jumping
// note: no endurance>0 check because it feels odd if we can't jump
moveDir.y = jumpSpeed;
PlayJumpSound();
return MoveState.AIRBORNE;
}
else if (EventMounted())
{
return MoveState.MOUNTED;
}
else if (EventCrouchToggle())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.5f, true, true, false);
return MoveState.CROUCHING;
}
else if (EventCrawlToggle())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.CRAWLING;
}
else if (EventUnderWater())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.SWIMMING;
}
else if (inputDir != Vector2.zero)
{
return MoveState.WALKING;
}
else if (EventDismounted()) {} // don't care
return MoveState.IDLE;
}
MoveState UpdateWALKINGandRUNNING(Vector2 inputDir, Vector3 desiredDir)
{
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// walk or run?
float speed = GetWalkOrRunSpeed();
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, speed, inputDir != Vector2.zero ? walkAcceleration : walkDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventFalling())
{
return MoveState.AIRBORNE;
}
else if (EventJumpRequested())
{
// start the jump movement into Y dir, go to jumping
// note: no endurance>0 check because it feels odd if we can't jump
moveDir.y = jumpSpeed;
PlayJumpSound();
return MoveState.AIRBORNE;
}
else if (EventMounted())
{
return MoveState.MOUNTED;
}
else if (EventCrouchToggle())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.5f, true, true, false);
return MoveState.CROUCHING;
}
else if (EventCrawlToggle())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.CRAWLING;
}
else if (EventUnderWater())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.SWIMMING;
}
// go to idle after fully decelerating (y doesn't matter)
else if (moveDir.x == 0 && moveDir.z == 0)
{
return MoveState.IDLE;
}
else if (EventDismounted()) {} // don't care
ProgressStepCycle(inputDir, speed);
return speed == walkSpeed ? MoveState.WALKING : MoveState.RUNNING;
}
MoveState UpdateCROUCHING(Vector2 inputDir, Vector3 desiredDir)
{
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, crouchSpeed, inputDir != Vector2.zero ? crouchAcceleration : crouchDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventFalling())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.AIRBORNE;
}
}
else if (EventJumpRequested())
{
// stop crouching when pressing jump key. this feels better than
// jumping from the crouching state.
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
}
else if (EventMounted())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.MOUNTED;
}
}
else if (EventCrouchToggle())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
}
else if (EventCrawlToggle())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.CRAWLING;
}
else if (EventUnderWater())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.SWIMMING;
}
else if (EventDismounted()) {} // don't care
ProgressStepCycle(inputDir, crouchSpeed);
return MoveState.CROUCHING;
}
MoveState UpdateCRAWLING(Vector2 inputDir, Vector3 desiredDir)
{
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, crawlSpeed, inputDir != Vector2.zero ? crawlAcceleration : crawlDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventFalling())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.AIRBORNE;
}
}
else if (EventJumpRequested())
{
// stop crawling when pressing jump key. this feels better than
// jumping from the crawling state.
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
}
else if (EventMounted())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.MOUNTED;
}
}
else if (EventCrouchToggle())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 0.5f, true))
{
controller.SetHeight(controller.defaultHeight * 0.5f, true, true, false);
return MoveState.CROUCHING;
}
}
else if (EventCrawlToggle())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
}
else if (EventUnderWater())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.SWIMMING;
}
else if (EventDismounted()) {} // don't care
ProgressStepCycle(inputDir, crawlSpeed);
return MoveState.CRAWLING;
}
MoveState UpdateAIRBORNE(Vector2 inputDir, Vector3 desiredDir)
{
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// walk or run?
float speed = GetWalkOrRunSpeed();
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, speed, inputDir != Vector2.zero ? airborneAcceleration : airborneDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventLanded())
{
// apply fall damage only in AIRBORNE->Landed.
// (e.g. not if we run face forward into a wall with high velocity)
ApplyFallDamage();
PlayLandingSound();
return MoveState.IDLE;
}
else if (EventUnderWater())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.SWIMMING;
}
return MoveState.AIRBORNE;
}
MoveState UpdateCLIMBING(Vector2 inputDir, Vector3 desiredDir)
{
if (EventDied())
{
// player rotation was adjusted to ladder rotation before.
// let's reset it, but also keep look forward
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
ladderCollider = null;
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
// finished climbing?
else if (EventLadderExit())
{
// player rotation was adjusted to ladder rotation before.
// let's reset it, but also keep look forward
transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
ladderCollider = null;
return MoveState.IDLE;
}
// interpret forward/backward movement as upward/downward
// with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, climbSpeed, inputDir != Vector2.zero ? climbAcceleration : climbDeceleration);
moveDir.x = inputDir.x * horizontalSpeed;
moveDir.y = inputDir.y * horizontalSpeed;
moveDir.z = 0;
// make the direction relative to ladder rotation. so when pressing right
// we always climb to the right of the ladder, no matter how it's rotated
moveDir = ladderCollider.transform.rotation * moveDir;
Debug.DrawLine(transform.position, transform.position + moveDir, Color.yellow, 0.1f, false);
return MoveState.CLIMBING;
}
MoveState UpdateSWIMMING(Vector2 inputDir, Vector3 desiredDir)
{
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
// not under water anymore?
else if (!EventUnderWater())
{
// rescale capsule if possible
if (controller.CanSetHeight(controller.defaultHeight * 1f, true))
{
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
}
// horizontal2 input axis rotates the character via QE keys
transform.Rotate(Vector3.up * Input.GetAxis("Horizontal2") * rotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, swimSpeed, inputDir != Vector2.zero ? swimAcceleration : swimDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.z = desiredDir.z * horizontalSpeed;
// gravitate toward surface
if (waterCollider != null)
{
float surface = waterCollider.bounds.max.y;
float surfaceDirection = surface - controller.bounds.min.y - swimSurfaceOffset;
moveDir.y = surfaceDirection * swimSpeed;
}
else moveDir.y = 0;
return MoveState.SWIMMING;
}
MoveState UpdateMOUNTED(Vector2 inputDir, Vector3 desiredDir)
{
// walk or run?
float speed = GetWalkOrRunSpeed();
// recalculate desired direction while ignoring inputDir horizontal part
// (horses can't strafe.)
desiredDir = GetDesiredDirection(new Vector2(0, inputDir.y));
// horizontal input axis rotates the character instead of strafing
transform.Rotate(Vector3.up * inputDir.x * mountedRotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, speed, inputDir != Vector2.zero ? mountedAcceleration : mountedDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventFalling())
{
return MoveState.MOUNTED_AIRBORNE;
}
else if (EventJumpRequested())
{
// start the jump movement into Y dir, go to jumping
// note: no endurance>0 check because it feels odd if we can't jump
moveDir.y = jumpSpeed;
PlayJumpSound();
return MoveState.MOUNTED_AIRBORNE;
}
else if (EventDismounted())
{
return MoveState.IDLE;
}
else if (EventUnderWater())
{
return MoveState.MOUNTED_SWIMMING;
}
else if (EventMounted()) {} // don't care
return MoveState.MOUNTED;
}
MoveState UpdateMOUNTED_AIRBORNE(Vector2 inputDir, Vector3 desiredDir)
{
// walk or run?
float speed = GetWalkOrRunSpeed();
// recalculate desired direction while ignoring inputDir horizontal part
// (horses can't strafe.)
desiredDir = GetDesiredDirection(new Vector2(0, inputDir.y));
// horizontal input axis rotates the character instead of strafing
transform.Rotate(Vector3.up * inputDir.x * mountedRotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, speed, inputDir != Vector2.zero ? airborneAcceleration : airborneDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = desiredDir.z * horizontalSpeed;
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
else if (EventLanded())
{
// apply fall damage only in AIRBORNE->Landed.
// (e.g. not if we run face forward into a wall with high velocity)
ApplyFallDamage();
PlayLandingSound();
return MoveState.MOUNTED;
}
else if (EventUnderWater())
{
return MoveState.MOUNTED_SWIMMING;
}
return MoveState.MOUNTED_AIRBORNE;
}
MoveState UpdateMOUNTED_SWIMMING(Vector2 inputDir, Vector3 desiredDir)
{
if (EventDied())
{
// rescale capsule
controller.SetHeight(controller.defaultHeight * 0.25f, true, true, false);
return MoveState.DEAD;
}
// not under water anymore?
else if (!EventUnderWater())
{
return MoveState.MOUNTED;
}
// dismounted while swimming?
else if (EventDismounted())
{
return MoveState.SWIMMING;
}
// recalculate desired direction while ignoring inputDir horizontal part
// (horses can't strafe.)
desiredDir = GetDesiredDirection(new Vector2(0, inputDir.y));
// horizontal input axis rotates the character instead of strafing
transform.Rotate(Vector3.up * inputDir.x * mountedRotationSpeed * Time.deltaTime);
// move with acceleration (feels better)
horizontalSpeed = AccelerateSpeed(inputDir, horizontalSpeed, swimSpeed, inputDir != Vector2.zero ? swimAcceleration : swimDeceleration);
moveDir.x = desiredDir.x * horizontalSpeed;
moveDir.z = desiredDir.z * horizontalSpeed;
// move
moveDir.x = desiredDir.x * swimSpeed;
moveDir.z = desiredDir.z * swimSpeed;
// gravitate toward surface
if (waterCollider != null)
{
float surface = waterCollider.bounds.max.y;
float surfaceDirection = surface - controller.bounds.min.y + mountUsage.seatOffsetY - swimSurfaceOffset;
moveDir.y = surfaceDirection * swimSpeed;
}
else moveDir.y = 0;
return MoveState.MOUNTED_SWIMMING;
}
MoveState UpdateDEAD(Vector2 inputDir, Vector3 desiredDir)
{
// keep falling while dead: if we get shot while falling, we shouldn't
// stop in mid air
moveDir.x = 0;
moveDir.y = ApplyGravity(moveDir.y);
moveDir.z = 0;
// not dead anymore?
if (health.current > 0)
{
// rescale capsule in any case. we don't check CanSetHeight so that
// no other entities can block a respawn. SetHeight will depenetrate
// either way.
controller.SetHeight(controller.defaultHeight * 1f, true, true, false);
return MoveState.IDLE;
}
return MoveState.DEAD;
}
// Update is called once per frame
void Update()
{
// get input and desired direction based on camera and ground
Vector2 inputDir = GetInputDirection();
Vector3 desiredDir = GetDesiredDirection(inputDir);
Vector3 desiredGroundDir = GetDesiredDirectionOnGround(desiredDir);
Debug.DrawLine(transform.position, transform.position + new Vector3(inputDir.x, 0, inputDir.y), Color.green);
Debug.DrawLine(transform.position, transform.position + desiredDir, Color.blue);
//Debug.DrawLine(transform.position, transform.position + desiredGroundDir, Color.cyan);
// update state machine
if (state == MoveState.IDLE) state = UpdateIDLE(inputDir, desiredGroundDir);
else if (state == MoveState.WALKING) state = UpdateWALKINGandRUNNING(inputDir, desiredGroundDir);
else if (state == MoveState.RUNNING) state = UpdateWALKINGandRUNNING(inputDir, desiredGroundDir);
else if (state == MoveState.CROUCHING) state = UpdateCROUCHING(inputDir, desiredGroundDir);
else if (state == MoveState.CRAWLING) state = UpdateCRAWLING(inputDir, desiredGroundDir);
else if (state == MoveState.AIRBORNE) state = UpdateAIRBORNE(inputDir, desiredGroundDir);
else if (state == MoveState.CLIMBING) state = UpdateCLIMBING(inputDir, desiredGroundDir);
else if (state == MoveState.SWIMMING) state = UpdateSWIMMING(inputDir, desiredGroundDir);
else if (state == MoveState.MOUNTED) state = UpdateMOUNTED(inputDir, desiredGroundDir);
else if (state == MoveState.MOUNTED_AIRBORNE) state = UpdateMOUNTED_AIRBORNE(inputDir, desiredGroundDir);
else if (state == MoveState.MOUNTED_SWIMMING) state = UpdateMOUNTED_SWIMMING(inputDir, desiredGroundDir);
else if (state == MoveState.DEAD) state = UpdateDEAD(inputDir, desiredGroundDir);
else Debug.LogError("Unhandled Movement State: " + state);
// cache this move's state to detect landing etc. next time
if (!controller.isGrounded) lastFall = controller.velocity;
// move depending on latest moveDir changes
controller.Move(moveDir * Time.deltaTime); // note: returns CollisionFlags if needed
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
// (This code is reliant on the specific run cycle offset in our animations,
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
float runCycle = Mathf.Repeat(animator.GetCurrentAnimatorStateInfo(0).normalizedTime + runCycleLegOffset, 1);
jumpLeg = (runCycle < 0.5f ? 1 : -1);// * move.z;
}
void PlayLandingSound()
{
feetAudio.clip = landSound;
feetAudio.Play();
nextStep = stepCycle + .5f;
}
void PlayJumpSound()
{
feetAudio.clip = jumpSound;
feetAudio.Play();
}
void ProgressStepCycle(Vector3 inputDir, float speed)
{
if (controller.velocity.sqrMagnitude > 0 && (inputDir.x != 0 || inputDir.y != 0))
{
stepCycle += (controller.velocity.magnitude + (speed*(state == MoveState.WALKING ? 1 : runStepLength)))*
Time.deltaTime;
}
if (stepCycle > nextStep)
{
nextStep = stepCycle + runStepInterval;
PlayFootStepAudio();
}
}
void PlayFootStepAudio()
{
if (!controller.isGrounded) return;
// pick & play a random footstep sound from the array,
// excluding sound at index 0
int n = Random.Range(1, footstepSounds.Length);
feetAudio.clip = footstepSounds[n];
feetAudio.PlayOneShot(feetAudio.clip);
// move picked sound to index 0 so it's not picked next time
footstepSounds[n] = footstepSounds[0];
footstepSounds[0] = feetAudio.clip;
}
void OnTriggerEnter(Collider co)
{
if (co.tag == "Ladder" &&
!mountUsage.IsMounted() &&
state != MoveState.CLIMBING) // only do initialization once
{
state = MoveState.CLIMBING;
ladderCollider = co;
// make player look directly at ladder forward. but we also initialize
// freelook manually already to overwrite the initial rotation, so
// that in the end, the camera keeps looking at the same angle even
// though we did modify transform.forward.
// note: even though we set the rotation perfectly here, there's
// still one frame where it seems to interpolate between the
// new and the old rotation, which causes 1 odd camera frame.
// this could be avoided by overwriting transform.forward once
// more in LateUpdate.
look.InitializeFreeLook();
Quaternion original = transform.rotation;
transform.forward = co.transform.forward;
}
// touching water? then set water collider
if (co.tag == "Water")
{
waterCollider = co;
}
}
void OnTriggerExit(Collider co)
{
// not touching water anymore? then clear water collider
if (co.tag == "Water")
{
waterCollider = null;
}
}
}