-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerAllTerrainVehicle.cpp
144 lines (116 loc) · 5.14 KB
/
PlayerAllTerrainVehicle.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
// Sample C++ code from UE5 vehicle project by Ian Snyder
#include "PlayerAllTerrainVehicle.h"
#include "GameFramework/PlayerInput.h"
#include "Components/InputComponent.h"
APlayerAllTerrainVehicle::APlayerAllTerrainVehicle()
{
// Create a spring-arm attached to the ATV mesh.
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->bDoCollisionTest = false;
SpringArm->SetUsingAbsoluteRotation(false);
SpringArm->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
SpringArm->TargetArmLength = 1000.0f;
SpringArm->bEnableCameraLag = false;
SpringArm->CameraLagSpeed = 5.0f;
SpringArm->SetupAttachment(ATVMesh);
// Create a camera and attach to the spring-arm.
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->bUsePawnControlRotation = false;
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
}
// Init default pawn input bindings
static void InitializeDefaultPawnInputBindings()
{
static bool bindingsAdded = false;
if (bindingsAdded == false)
{
bindingsAdded = true;
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_MoveForward", EKeys::W, 1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_MoveForward", EKeys::S, -1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_MoveForward", EKeys::Up, 1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_MoveForward", EKeys::Down, -1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_MoveForward", EKeys::Gamepad_LeftY, 1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_Turn", EKeys::A, -1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_Turn", EKeys::D, 1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_Turn", EKeys::Left, -1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_Turn", EKeys::Right, 1.f));
UPlayerInput::AddEngineDefinedAxisMapping(FInputAxisKeyMapping("AllTerrainVehicle_Turn", EKeys::Gamepad_LeftX, 1.f));
UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping("AllTerrainVehicle_Jump", EKeys::Enter));
UPlayerInput::AddEngineDefinedActionMapping(FInputActionKeyMapping("AllTerrainVehicle_Dash", EKeys::SpaceBar));
}
}
void APlayerAllTerrainVehicle::Tick(const float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
// Speed handling
FVector Velocity = ATVMesh->GetPhysicsLinearVelocity();
const float z = Velocity.Z; // Store the z value so we can modify the X and Y e-Z-ly ;)
Velocity.Z = 0.0f;
if (Velocity.Size() > MaximumSpeed * 100.0f)
{
Velocity.Normalize();
Velocity *= MaximumSpeed * 100.0f;
Velocity.Z = z;
const float BrakingRatio = FMath::Pow(1.0f - FMath::Min(TempDashTimer, 1.0f), 2.0f);
const FVector MergedVelocity = FMath::Lerp(ATVMesh->GetPhysicsLinearVelocity(), Velocity, BrakingRatio);
ATVMesh->SetPhysicsLinearVelocity(MergedVelocity);
} else
{
ATVMesh->AddForce( ATVMesh->GetForwardVector() * InputForward * ControllerForce * ATVMesh->GetMass());
}
// Turn handling
FRotator NewRotator = ATVMesh->GetRelativeRotation();
ATVMesh->SetRelativeRotation( NewRotator.Add(0.0f, InputTurn * 1.0f, 0.0f), false, nullptr, ETeleportType::TeleportPhysics );
if (TempDashTimer > 0.0f)
{
TempDashTimer = FMath::Max(0.0f, TempDashTimer - DeltaSeconds);
}
// DEBUG Drawing debug lines for fun and profit!
if(DrawDebugVectorLines)
{
DrawDebugLine( GetWorld(),
ATVMesh->GetComponentLocation(),
ATVMesh->GetComponentLocation() + (ATVMesh->GetComponentVelocity() * DebugLineLength),
DebugLineColor,
false,
DebugLineTime,
0,
DebugLineThickness );
}
}
void APlayerAllTerrainVehicle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent != nullptr);
Super::SetupPlayerInputComponent(PlayerInputComponent);
InitializeDefaultPawnInputBindings();
PlayerInputComponent->BindAxis("AllTerrainVehicle_MoveForward", this, &APlayerAllTerrainVehicle::MoveForward);
PlayerInputComponent->BindAxis("AllTerrainVehicle_Turn", this, &APlayerAllTerrainVehicle::Turn);
PlayerInputComponent->BindAction("AllTerrainVehicle_Jump", EInputEvent::IE_Pressed, this, &APlayerAllTerrainVehicle::Jump);
PlayerInputComponent->BindAction("AllTerrainVehicle_Dash", EInputEvent::IE_Pressed, this, &APlayerAllTerrainVehicle::Dash);
}
void APlayerAllTerrainVehicle::Jump()
{
// Only jump if we're in contact with something
if(InContact)
{
// Add the impulse to the ATV to perform the jump
ATVMesh->AddImpulse(FVector::UpVector * JumpForce);
}
}
void APlayerAllTerrainVehicle::Dash()
{
// Only dash if we're not dashing already.
if (TempDashTimer == 0.0f)
{
// Only dash if we have an existing velocity vector to dash towards
FVector velocity = ATVMesh->GetPhysicsLinearVelocity();
if (velocity.Length() > 1.0f)
{
velocity.Normalize();
ATVMesh->AddImpulse(velocity * DashForce * 1000.0f);
// Set how long the dash lasts
TempDashTimer = DashTime;
}
}
}