-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomCharacterMovementComponent.cpp
769 lines (623 loc) · 29.8 KB
/
CustomCharacterMovementComponent.cpp
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainCharacter/CustomCharacterMovementComponent.h"
#include "Components/CapsuleComponent.h"
#include "Kismet/GameplayStatics.h"
#include "MainCharacter/MainCharacter.h"
#include "Utility/States/StateIdle.h"
#include "Utility/Transition.h"
#include "Utility/States/StateCrouching.h"
#include "Utility/States/StateDash.h"
#include "Utility/States/StateJumping.h"
#include "Utility/States/StateRunning.h"
#include "Utility/States/StateVault.h"
#include "Utility/States/StateWalking.h"
#include "Utility/States/StateMantle.h"
// Shortcut macros
#if 0
float Duration = 5.0f;
#define PRINT_SCREEN(x) GEngine->AddOnScreenDebugMessage(-1, Duration ? Duration : -1.f, FColor::Yellow, x);
#define DRAW_POINT(x, c) DrawDebugPoint(GetWorld(), x, 10, c, !Duration, Duration);
#define DRAW_LINE(x1, x2, c) DrawDebugLine(GetWorld(), x1, x2, c, !Duration, Duration);
#define DRAW_CAPSULE(x, c) DrawDebugCapsule(GetWorld(), x, GetCapsuleHalfHeight(), GetCapsuleRadius(), FQuat::Identity, c, !Duration, Duration);
#else
#define PRINT_SCREEN(x)
#define DRAW_POINT(x, c)
#define DRAW_LINE(x1, x2, c)
#define DRAW_CAPSULE(x, c)
#endif
// CONSTRUCTOR
UCustomCharacterMovementComponent::UCustomCharacterMovementComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
NavAgentProps.bCanCrouch = true;
bCanWalkOffLedgesWhenCrouching = true;
}
// BEGIN PLAY
void UCustomCharacterMovementComponent::BeginPlay()
{
Super::BeginPlay();
MainCharacter = Cast<AMainCharacter>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
SetCrouchedHalfHeight(Crouch_HalfHeight);
GravityScale = CustomGravity;
BuildStateMachine();
}
// STATE MACHINE
// Custom state machine that handles all the different movement states
#pragma region STATE_MACHINE
// Called at begin play to create and setup the movement state machine
void UCustomCharacterMovementComponent::BuildStateMachine()
{
// Create the state machine object
StateMachine = NewObject<UStateMachine>();
// Create the states and add them to the state machine
TObjectPtr<UStateIdle> StateIdle = NewObject<UStateIdle>();
StateMachine->AddState(StateIdle);
StateIdle->Initialize(this, ECustomMovementState::Idle);
TObjectPtr<UStateWalking> StateWalking = NewObject<UStateWalking>();
StateMachine->AddState(StateWalking);
StateWalking->Initialize(this, ECustomMovementState::Walking);
TObjectPtr<UStateRunning> StateRunning = NewObject<UStateRunning>();
StateMachine->AddState(StateRunning);
StateRunning->Initialize(this, ECustomMovementState::Running);
TObjectPtr<UStateJumping> StateJumping = NewObject<UStateJumping>();
StateMachine->AddState(StateJumping);
StateJumping->Initialize(this, ECustomMovementState::Jumping);
TObjectPtr<UStateCrouching> StateCrouching = NewObject<UStateCrouching>();
StateMachine->AddState(StateCrouching);
StateCrouching->Initialize(this, ECustomMovementState::Crouching);
TObjectPtr<UStateMantle> StateMantle = NewObject<UStateMantle>();
StateMachine->AddState(StateMantle);
StateMantle->Initialize(this, ECustomMovementState::Mantling);
TObjectPtr<UStateVault> StateVault = NewObject<UStateVault>();
StateMachine->AddState(StateVault);
StateVault->Initialize(this, ECustomMovementState::Vaulting);
TObjectPtr<UStateDash> StateDash = NewObject<UStateDash>();
StateMachine->AddState(StateDash);
StateDash->Initialize(this, ECustomMovementState::Dashing);
StateMachine->Init(StateIdle);
// Create the transitions and add them to the state machine
// Bind each transition delegate with a function used to check if the transition is true/false
// Set the state that transition points to
// IDLE
// Idle TO Walking
TObjectPtr<UTransition> IdleToWalking = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToWalking);
IdleToWalking->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanWalkFromIdle);
IdleToWalking->Init(StateWalking);
// Idle TO Jumping
TObjectPtr<UTransition> IdleToJumping = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToJumping);
IdleToJumping->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanJumpFromIdle);
IdleToJumping->Init(StateJumping);
// Idle TO Crouching
TObjectPtr<UTransition> IdleToCrouching = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToCrouching);
IdleToCrouching->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanCrouchFromIdle);
IdleToCrouching->Init(StateCrouching);
// Idle TO Running
TObjectPtr<UTransition> IdleToRunning = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToRunning);
IdleToRunning->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanRunFromIdle);
IdleToRunning->Init(StateRunning);
// Idle TO Mantle
TObjectPtr<UTransition> IdleToMantle = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToMantle);
IdleToMantle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanMantleFromAny);
IdleToMantle->Init(StateMantle);
// Idle TO Vault
TObjectPtr<UTransition> IdleToVault = NewObject<UTransition>();
StateIdle->Transitions.Add(IdleToVault);
IdleToVault->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanVaultFromAny);
IdleToVault->Init(StateVault);
// WALKING
// Walking TO Idle
TObjectPtr<UTransition> WalkingToIdle = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToIdle);
WalkingToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromWalk);
WalkingToIdle->Init(StateIdle);
// Walking TO Running
TObjectPtr<UTransition> WalkingToRunning = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToRunning);
WalkingToRunning->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanRunFromWalk);
WalkingToRunning->Init(StateRunning);
// Walking TO Crouching
TObjectPtr<UTransition> WalkingToCrouching = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToCrouching);
WalkingToCrouching->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanCrouchFromWalk);
WalkingToCrouching->Init(StateCrouching);
// Walking TO Jump
TObjectPtr<UTransition> WalkingToJump = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToJump);
WalkingToJump->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanJumpFromWalk);
WalkingToJump->Init(StateJumping);
// Walking TO Mantle
TObjectPtr<UTransition> WalkingToMantle = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToMantle);
WalkingToMantle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanMantleFromAny);
WalkingToMantle->Init(StateMantle);
// Walking TO Vault
TObjectPtr<UTransition> WalkingToVault = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToVault);
WalkingToVault->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanVaultFromAny);
WalkingToVault->Init(StateVault);
// Walking TO Dash
TObjectPtr<UTransition> WalkingToDash = NewObject<UTransition>();
StateWalking->Transitions.Add(WalkingToDash);
WalkingToDash->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanDashFromAny);
WalkingToDash->Init(StateDash);
// RUNNING
// Running TO Idle
TObjectPtr<UTransition> RunningToIdle = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToIdle);
RunningToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromRun);
RunningToIdle->Init(StateIdle);
// Running TO Walking
TObjectPtr<UTransition> RunningToWalking = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToWalking);
RunningToWalking->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanWalkFromRun);
RunningToWalking->Init(StateWalking);
// Running TO Jump
TObjectPtr<UTransition> RunningToJump = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToJump);
RunningToJump->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanJumpFromRun);
RunningToJump->Init(StateJumping);
// Running TO Mantle
TObjectPtr<UTransition> RunningToMantle = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToMantle);
RunningToMantle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanMantleFromAny);
RunningToMantle->Init(StateMantle);
// Running TO Vault
TObjectPtr<UTransition> RunningToVault = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToVault);
RunningToVault->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanVaultFromAny);
RunningToVault->Init(StateVault);
// Running TO Dash
TObjectPtr<UTransition> RunningToDash = NewObject<UTransition>();
StateRunning->Transitions.Add(RunningToDash);
RunningToDash->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanDashFromAny);
RunningToDash->Init(StateDash);
// JUMP
// Jump TO Idle
TObjectPtr<UTransition> JumpToIdle = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToIdle);
JumpToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromJump);
JumpToIdle->Init(StateIdle);
// Jump TO Walking
TObjectPtr<UTransition> JumpToWalking = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToWalking);
JumpToWalking->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanWalkFromJump);
JumpToWalking->Init(StateWalking);
// Jump TO Running
TObjectPtr<UTransition> JumpToRunning = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToRunning);
JumpToRunning->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanRunFromJump);
JumpToRunning->Init(StateRunning);
// Jump TO Crouch
TObjectPtr<UTransition> JumpToCrouch = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToCrouch);
JumpToCrouch->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanCrouchFromJump);
JumpToCrouch->Init(StateCrouching);
// Jump TO Mantle
TObjectPtr<UTransition> JumpToMantle = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToMantle);
JumpToMantle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::TryMantle);
JumpToMantle->Init(StateMantle);
// Jump TO Jump
TObjectPtr<UTransition> JumpToJump = NewObject<UTransition>();
StateJumping->Transitions.Add(JumpToJump);
JumpToJump->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanJumpFromJump);
JumpToJump->Init(StateJumping);
// CROUCH
// Crouch TO Idle
TObjectPtr<UTransition> CrouchToIdle = NewObject<UTransition>();
StateCrouching->Transitions.Add(CrouchToIdle);
CrouchToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromCrouch);
CrouchToIdle->Init(StateIdle);
// Crouch TO Walking
TObjectPtr<UTransition> CrouchToWalking = NewObject<UTransition>();
StateCrouching->Transitions.Add(CrouchToWalking);
CrouchToWalking->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanWalkFromCrouch);
CrouchToWalking->Init(StateWalking);
// Crouch TO Running
TObjectPtr<UTransition> CrouchToRunning = NewObject<UTransition>();
StateCrouching->Transitions.Add(CrouchToRunning);
CrouchToRunning->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanRunFromCrouch);
CrouchToRunning->Init(StateRunning);
// Crouch TO Jump
TObjectPtr<UTransition> CrouchToJump = NewObject<UTransition>();
StateCrouching->Transitions.Add(CrouchToJump);
CrouchToJump->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanJumpFromCrouch);
CrouchToJump->Init(StateJumping);
// MANTLE
// Mantle TO Idle
TObjectPtr<UTransition> MantleToJump = NewObject<UTransition>();
StateMantle->Transitions.Add(MantleToJump);
MantleToJump->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromMantle);
MantleToJump->Init(StateIdle);
// VAULT
// Vault TO Idle
TObjectPtr<UTransition> VaultToIdle = NewObject<UTransition>();
StateVault->Transitions.Add(VaultToIdle);
VaultToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromVault);
VaultToIdle->Init(StateIdle);
// DASH
// Dash TO Idle
TObjectPtr<UTransition> DashToIdle = NewObject<UTransition>();
StateDash->Transitions.Add(DashToIdle);
DashToIdle->OnCheckConditionDelegate.BindUObject(this, &UCustomCharacterMovementComponent::CanIdleFromDash);
DashToIdle->Init(StateIdle);
}
void UCustomCharacterMovementComponent::SetCurrentMovementState(ECustomMovementState NewState)
{
CurrentMovementState = NewState;
}
void UCustomCharacterMovementComponent::SetLastMovementState(ECustomMovementState NewState)
{
LastMovementState = NewState;
}
// STATE MACHINE CONDITION CHECKER METHODS
// These methods are used by the state machine to check if a state can transition to another one and under what conditions
// FROM IDLE
bool UCustomCharacterMovementComponent::CanWalkFromIdle() const
{
return IsMovingOnGround() && !Velocity.IsZero();
}
bool UCustomCharacterMovementComponent::CanRunFromIdle() const
{
return bWantsToRun && !Velocity.IsZero();
}
bool UCustomCharacterMovementComponent::CanCrouchFromIdle() const
{
return bWantsToCrouchCustom;
}
bool UCustomCharacterMovementComponent::CanJumpFromIdle()
{
return bWantsToJump && !TryVault() && !TryMantle();
}
// FROM WALKING
bool UCustomCharacterMovementComponent::CanIdleFromWalk() const
{
return IsMovingOnGround() && Velocity.IsZero();
}
bool UCustomCharacterMovementComponent::CanRunFromWalk() const
{
return IsMovingOnGround() && bWantsToRun;
}
bool UCustomCharacterMovementComponent::CanCrouchFromWalk() const
{
return IsMovingOnGround() && bWantsToCrouchCustom;
}
bool UCustomCharacterMovementComponent::CanJumpFromWalk()
{
return bWantsToJump && !TryVault() && !TryMantle();
}
// FROM RUNNING
bool UCustomCharacterMovementComponent::CanIdleFromRun() const
{
return IsMovingOnGround() && Velocity.IsZero() && !bWantsToRun;
}
bool UCustomCharacterMovementComponent::CanWalkFromRun() const
{
return IsMovingOnGround() && !bWantsToRun;
}
bool UCustomCharacterMovementComponent::CanJumpFromRun()
{
return bWantsToJump && !TryVault() && !TryMantle();
}
// FROM JUMP
bool UCustomCharacterMovementComponent::CanIdleFromJump() const
{
return IsMovingOnGround() && Velocity.IsZero() && !bWantsToJump;
}
bool UCustomCharacterMovementComponent::CanWalkFromJump() const
{
return IsMovingOnGround() && !bWantsToJump;
}
bool UCustomCharacterMovementComponent::CanRunFromJump() const
{
return IsMovingOnGround() && !Velocity.IsZero() && bWantsToRun;
}
bool UCustomCharacterMovementComponent::CanCrouchFromJump() const
{
return IsMovingOnGround() && bWantsToCrouchCustom && !bWantsToJump;
}
bool UCustomCharacterMovementComponent::CanJumpFromJump() const
{
return bCanDoubleJump && bWantsToJump && MovementMode == MOVE_Falling;
}
// FROM CROUCH
bool UCustomCharacterMovementComponent::CanIdleFromCrouch()
{
return IsMovingOnGround() && Velocity.IsZero() && !bWantsToCrouchCustom && CanUncrouch();
}
bool UCustomCharacterMovementComponent::CanWalkFromCrouch()
{
return IsMovingOnGround() && !Velocity.IsZero() && !bWantsToCrouchCustom && CanUncrouch();
}
bool UCustomCharacterMovementComponent::CanRunFromCrouch()
{
return IsMovingOnGround() && bWantsToRun && CanUncrouch();
}
bool UCustomCharacterMovementComponent::CanJumpFromCrouch()
{
return bWantsToJump && CanUncrouch();
}
// FROM MANTLE
bool UCustomCharacterMovementComponent::CanIdleFromMantle() const
{
return !bCanMantle;
}
// FROM VAULT
bool UCustomCharacterMovementComponent::CanIdleFromVault() const
{
return !bCanVault;
}
// FROM DASH
bool UCustomCharacterMovementComponent::CanIdleFromDash() const
{
return !bWantsToDash;
}
// FROM ANY
bool UCustomCharacterMovementComponent::CanMantleFromAny() const
{
return bCanMantle && !bCanVault;
}
bool UCustomCharacterMovementComponent::CanVaultFromAny() const
{
return bCanVault && !bCanMantle;
}
bool UCustomCharacterMovementComponent::CanDashFromAny() const
{
return bWantsToDash;
}
// Used to check if there is enough space over the player when trying to exit from the CROUCH state
bool UCustomCharacterMovementComponent::CanUncrouch()
{
FVector BaseLocation = UpdatedComponent->GetComponentLocation() + ((GetCapsuleHalfHeight() - 10) * FVector::UpVector);
FCollisionShape CapsuleShape = FCollisionShape::MakeCapsule(GetCapsuleRadius(), MainCharacter->GetUncrouchedCapsuleHalfHeight());
auto Params = MainCharacter->GetIgnoreCharacterParams();
if (GetWorld()->OverlapAnyTestByProfile(BaseLocation, FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
bWantsToCrouchCustom = true;
return false;
}
return true;
}
#pragma endregion STATE_MACHINE
// Called every frame
void UCustomCharacterMovementComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Each tick the state machine checks if, for the current state a transition has become true and eventually performs the transition towards that state
StateMachine->Tick();
}
// CURRENT STATE GETTER
ECustomMovementState UCustomCharacterMovementComponent::GetCurrentMovementState() const
{
return CurrentMovementState;
}
ECustomMovementState UCustomCharacterMovementComponent::GetLastMovementState() const
{
return LastMovementState;
}
float UCustomCharacterMovementComponent::GetCapsuleRadius() const
{
return MainCharacter->GetCapsuleComponent()->GetScaledCapsuleRadius();
}
float UCustomCharacterMovementComponent::GetCapsuleHalfHeight() const
{
return MainCharacter->GetCapsuleComponent()->GetScaledCapsuleHalfHeight();
}
// MOVEMENT
#pragma region MOVEMENT
void UCustomCharacterMovementComponent::SprintPressed()
{
bWantsToRun = true;
}
void UCustomCharacterMovementComponent::SprintReleased()
{
bWantsToRun = false;
}
void UCustomCharacterMovementComponent::JumpPressed()
{
bWantsToJump = true;
}
void UCustomCharacterMovementComponent::CrouchPressed()
{
if (CurrentMovementState == ECustomMovementState::Jumping || CurrentMovementState == ECustomMovementState::Running) return;
bWantsToCrouchCustom = !bWantsToCrouchCustom;
}
void UCustomCharacterMovementComponent::DashPressed()
{
if (CurrentMovementState == ECustomMovementState::Crouching || CurrentMovementState == ECustomMovementState::Jumping) return;
bWantsToDash = !bWantsToDash;
}
#pragma endregion
// MANTLE SYSTEM
// The mantle system is used to check if (when jumping/in the jump state) there is an object in front of the character over which he can climb and cannot vault over
// It uses various raycast to find the object, its height and potential steepness, and if there is enough space over it for the player
// Lastly it differentiates between a high/low mantle and set a boolean variable to true for the custom state machine to perform the transition
// The implementation for the mantle is done in blueprint since I use a timeline to perform the movement while the setup for that is done in the "StateMantle.cpp"
#pragma region MANTLE
bool UCustomCharacterMovementComponent::TryMantle()
{
if (bCanVault) return false;
// Location of the base of the capsule
FVector BaseLocation = UpdatedComponent->GetComponentLocation() + FVector::DownVector * GetCapsuleHalfHeight();
// Forward vector
FVector Forward = UpdatedComponent->GetForwardVector().GetSafeNormal2D();
// Actors to ignore
auto Params = MainCharacter->GetIgnoreCharacterParams();
// Max height reachable by the mantle
float MaxHeight = GetCapsuleHalfHeight() * 2 + MantleReachHeight;
float CosMantleMinWallSteepnessAngle = FMath::Cos(FMath::DegreesToRadians(MantleMinWallSteepnessAngle));
float CosMantleMaxSurfaceAngle = FMath::Cos(FMath::DegreesToRadians(MantleMaxSurfaceAngle));
float CosMantleMaxAlignmentAngle = FMath::Cos(FMath::DegreesToRadians(MantleMaxAlignmentAngle));
// CHECK OBSTACLE FRONT FACE
FHitResult FrontHit;
// Scale the distance in which we check for a possible hit with the velocity of the character
//float CheckDistance = FMath::Clamp(Velocity | Forward, GetCapsuleRadius() + 30, MantleMaxDistance);
float CheckDistance = GetCapsuleRadius() + MantleMaxDistance;
// The starting point of the line trace takes into account the step height and a customisable offset
FVector FrontStart = BaseLocation + FVector::UpVector * (MaxStepHeight - 1);
FrontStart.Z += MantleBaseStartOffset;
for (int i = 0; i < 10; i++)
{
if (GetWorld()->LineTraceSingleByProfile(FrontHit, FrontStart, FrontStart + Forward * CheckDistance, "BlockAll", Params)) break;
FrontStart += FVector::UpVector * (2.f * GetCapsuleHalfHeight() - (MaxStepHeight - 1)) / 9;
}
if (!FrontHit.IsValidBlockingHit()) return false;
float CosWallSteepnessAngle = FrontHit.Normal | FVector::UpVector;
// Check if the front of the object is too steep
if (FMath::Abs(CosWallSteepnessAngle) > CosMantleMinWallSteepnessAngle || (Forward | -FrontHit.Normal) < CosMantleMaxAlignmentAngle) return false;
DRAW_POINT(FrontHit.Location, FColor::Red)
// CHECK OBSTACLE HEIGHT
TArray<FHitResult> HeightHits;
FHitResult SurfaceHit;
// Project the UP vector onto the normal vector of the object hit and normalize it
// This give us a vector that goes UP in the direction of the wall
FVector WallUpVector = FVector::VectorPlaneProject(FVector::UpVector, FrontHit.Normal).GetSafeNormal();
float WallCos = FVector::UpVector | FrontHit.Normal;
float WallSin = FMath::Sqrt(1 - WallCos * WallCos);
// The starting point for the line trace is the location of the wall + a forward vector scaled by the WALL-UP vector times the magnitude we want this vector (the max height we can perform the mantle), all divided by WALL-SIN
// WALL-SIN will be between 0 and 1 thus making TRACE-START bigger to account for the potential steepness of the wall (to ensure that TRACE-START is always of the same length)
FVector TraceStart = FrontHit.Location + Forward + WallUpVector * (MaxHeight - (MaxStepHeight - 1)) / WallSin;
DRAW_POINT(TraceStart, FColor::Yellow);
DRAW_LINE(TraceStart, FrontHit.Location + Forward, FColor::Orange);
if(!GetWorld()->LineTraceMultiByProfile(HeightHits, TraceStart, FrontHit.Location + Forward, "BlockAll", Params)) return false;
for(const FHitResult& Hit : HeightHits)
{
// The tag is for the level designer to specify specific objects that should not be "mantable"
if (Hit.IsValidBlockingHit() && !Hit.GetActor()->ActorHasTag("NotMantle"))
{
SurfaceHit = Hit;
break;
}
}
// Check if the surface hit is valid OR if the surface is too steep to perform the mantle
if (!SurfaceHit.IsValidBlockingHit() || (SurfaceHit.Normal | FVector::UpVector) < CosMantleMaxSurfaceAngle) return false;
// We can consider the value of the dot product as the component of the first vector over the second one, thus giving us the height of the surface from the base location
float Height = (SurfaceHit.Location - BaseLocation) | FVector::UpVector;
DRAW_POINT(SurfaceHit.Location, FColor::Blue);
if (Height > MaxHeight) return false;
// CHECK CLEARANCE
float SurfaceCos = FVector::UpVector | SurfaceHit.Normal;
float SurfaceSin = FMath::Sqrt(1 - SurfaceCos * SurfaceCos);
// The point the capsule should mantle to, it takes into account the size of the capsule and any potential steepness of the surface
FVector ClearanceCapsuleLocation = SurfaceHit.Location + Forward * GetCapsuleRadius() + FVector::UpVector * (GetCapsuleHalfHeight() + 1 + GetCapsuleRadius() * 2 * SurfaceSin);
FCollisionShape CapsuleShape = FCollisionShape::MakeCapsule(GetCapsuleRadius(), GetCapsuleHalfHeight());
if (GetWorld()->OverlapAnyTestByProfile(ClearanceCapsuleLocation, FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
DRAW_CAPSULE(ClearanceCapsuleLocation, FColor::Red)
return false;
}
// Check if there are any obstacles between the player and the final point
if (GetWorld()->OverlapAnyTestByProfile(GetActorLocation() + FVector::UpVector * (GetCapsuleHalfHeight() + 1 + GetCapsuleRadius() * 2 * SurfaceSin), FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
DRAW_CAPSULE(ClearanceCapsuleLocation, FColor::Black)
return false;
}
DRAW_CAPSULE(SurfaceHit.Location + Forward * GetCapsuleRadius() + FVector::UpVector * GetCapsuleHalfHeight(), FColor::Green)
MantleLocation = SurfaceHit.Location + Forward * GetCapsuleRadius() + FVector::UpVector * GetCapsuleHalfHeight();
bCanMantle = true;
bHighMantle = true;
if (Height < LowMantleCutoff) bHighMantle = false;
return true;
}
#pragma endregion
// VAULT SYSTEM
// The vault system is used to check if (when jumping) there is an object in front of the character over which he can vault
// It uses various raycast (as for the mantle system) to find the object, its height, if there is enough space over it and most importantly if there is enough space on the other side of the object
// It differentiates between a normal vault and a "falling" one:
// - Normal vault: If on the other side of the object the ground is at a similar height than the starting side
// - Falling vault: If a normal one is not possible, this can be achieved only if the player runs or walks towards the obstacle, if it tries to vault from an IDLE state then a classic mantle would be performed
// The implementation for the vault is done in blueprint since I use a timeline to perform the movement while the setup for that is done in the "StateVault.cpp"
#pragma region VAULT
bool UCustomCharacterMovementComponent::TryVault()
{
// Location of the base of the capsule
FVector BaseLocation = UpdatedComponent->GetComponentLocation() + FVector::DownVector * GetCapsuleHalfHeight();
// Forward vector
FVector Forward = UpdatedComponent->GetForwardVector().GetSafeNormal2D();
// Actors to ignore
auto Params = MainCharacter->GetIgnoreCharacterParams();
FHitResult FrontHit;
// Scale the distance in which we check for a possible hit with the velocity of the character
float CheckDistance = FMath::Clamp(Velocity | Forward, GetCapsuleRadius() + 30, VaultMaxDistanceCheck);
// The starting point of the line trace takes into account the step height and a customisable offset
FVector FrontStart = BaseLocation + FVector::UpVector * (MaxStepHeight - 1);
for (int i = 0; i < 10; i++)
{
if (GetWorld()->LineTraceSingleByProfile(FrontHit, FrontStart, FrontStart + Forward * CheckDistance, "BlockAll", Params)) break;
FrontStart += FVector::UpVector * (GetCapsuleHalfHeight() - (MaxStepHeight - 1)) / 6;
}
if (!FrontHit.IsValidBlockingHit()) return false;
DRAW_POINT(FrontHit.Location, FColor::Red)
// CHECK OBSTACLE HEIGHT
TArray<FHitResult> HeightHits;
FHitResult SurfaceHit;
// Project the UP vector onto the normal vector of the object hit and normalize it
// This give us a vector that goes UP in the direction of the wall
FVector WallUpVector = FVector::VectorPlaneProject(FVector::UpVector, FrontHit.Normal).GetSafeNormal();
FVector TraceStart = FrontHit.Location + Forward + WallUpVector * (VaultMaxPossibleHeight - (MaxStepHeight - 1));
DRAW_POINT(TraceStart, FColor::Yellow)
DRAW_LINE(TraceStart, FrontHit.Location + Forward, FColor::Orange)
if(!GetWorld()->LineTraceMultiByProfile(HeightHits, TraceStart, FrontHit.Location + Forward, "BlockAll", Params)) return false;
for(const FHitResult& Hit : HeightHits)
{
// The tag is for the level designer to specify specific objects that should not be "vaultable"
if (Hit.IsValidBlockingHit() && !Hit.GetActor()->ActorHasTag("NotVault"))
{
SurfaceHit = Hit;
break;
}
}
float Height = (SurfaceHit.Location - BaseLocation) | FVector::UpVector;
DRAW_POINT(SurfaceHit.Location, FColor::Blue);
if (Height > VaultMaxPossibleHeight) return false;
// CHECK CLEARANCE
// The point the capsule should pass while vaulting
FVector ClearanceCapsuleLocation = SurfaceHit.Location + Forward * GetCapsuleRadius() + FVector::UpVector * GetCapsuleHalfHeight();
FCollisionShape CapsuleShape = FCollisionShape::MakeCapsule(GetCapsuleRadius(), GetCapsuleHalfHeight());
if (GetWorld()->OverlapAnyTestByProfile(ClearanceCapsuleLocation, FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
DRAW_CAPSULE(ClearanceCapsuleLocation, FColor::Red)
return false;
}
// Check if there are any obstacles between the player and the final point
if (GetWorld()->OverlapAnyTestByProfile(GetActorLocation() + FVector::UpVector * (GetCapsuleHalfHeight()), FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
DRAW_CAPSULE(ClearanceCapsuleLocation, FColor::Black)
return false;
}
FVector CapsuleFinalLocationOffset = SurfaceHit.Location + Forward * (GetCapsuleRadius() + VaultMaxPossibleWidth) + FVector::UpVector * (GetCapsuleHalfHeight() / 2);
// Check if there is enough space on the other side of the obstacle
if (GetWorld()->OverlapAnyTestByProfile(CapsuleFinalLocationOffset, FQuat::Identity, "BlockAll", CapsuleShape, Params))
{
DRAW_CAPSULE(CapsuleFinalLocationOffset, FColor::Silver)
return false;
}
// If on the other side of the object the ground is at a similar height than the starting point
FHitResult GroundHit;
TArray<AActor*> ActorsToIgnore;
VaultMiddleLocation = ClearanceCapsuleLocation - Forward * GetCapsuleRadius();
if (UKismetSystemLibrary::CapsuleTraceSingleByProfile(GetWorld(), CapsuleFinalLocationOffset, CapsuleFinalLocationOffset + FVector::DownVector * (GetCapsuleHalfHeight() / 2 + 15), GetCapsuleRadius(), GetCapsuleHalfHeight(), "BlockAll", false, ActorsToIgnore, EDrawDebugTrace::None ,GroundHit, true))
{
bFallingVault = false;
DRAW_CAPSULE(VaultMiddleLocation, FColor::Purple)
VaultLocation = GroundHit.Location;
DRAW_CAPSULE(VaultLocation, FColor::Green)
}
else
{
if (Velocity.IsZero()) return false;
bFallingVault = true;
VaultLocation = CapsuleFinalLocationOffset;
DRAW_CAPSULE(VaultLocation, FColor::Blue)
}
bCanVault = true;
return true;
}
#pragma endregion