-
Notifications
You must be signed in to change notification settings - Fork 2
/
SprintJump_autoCharacter.cpp
35 lines (30 loc) 路 1.21 KB
/
SprintJump_autoCharacter.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
// Part of jump mechanic study in https://arza-3d.github.io/UE4-jump-mechanic/
#include "SprintJump_autoCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/CapsuleComponent.h"
#include "Kismet/KismetMathLibrary.h"
ASprintJump_autoCharacter::ASprintJump_autoCharacter()
{
DefaultJumpZVelocity = GetCharacterMovement()->JumpZVelocity;
MaxJumpZVelocity = 1200.0f;
GradientJumpToSpeed = (MaxJumpZVelocity - DefaultJumpZVelocity) / GetCharacterMovement()->MaxWalkSpeed;
}
float ASprintJump_autoCharacter::GetXYSpeed()
{
FVector ActorVelocity = GetCapsuleComponent()->GetPhysicsLinearVelocity();
float xSpeedSquared = UKismetMathLibrary::Square(ActorVelocity.Component(0));
float ySpeedSquared = UKismetMathLibrary::Square(ActorVelocity.Component(1));
return UKismetMathLibrary::Sqrt(xSpeedSquared + ySpeedSquared);
}
void ASprintJump_autoCharacter::Jump()
{
float NewJumpZVelocity = GradientJumpToSpeed * GetXYSpeed() + DefaultJumpZVelocity;
GetCharacterMovement()->JumpZVelocity = NewJumpZVelocity;
Super::Jump();
}
void ASprintJump_autoCharacter::Landed(const FHitResult & Hit)
{
GetCharacterMovement()->JumpZVelocity = DefaultJumpZVelocity;
OnLanded(Hit);
LandedDelegate.Broadcast(Hit);
}