-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerController.cs
226 lines (162 loc) · 5.48 KB
/
playerController.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
using UnityEngine;
using UnityEngine.InputSystem;
namespace ArmadaDev.AceG
{
internal class playerController : MonoBehaviour
{
// References
CharacterController characterController;
Playerinputs playerInputs;
// Variables to store player's inputs
Vector2 currentInputsMovement;
Vector3 currentMovement;
bool isMovementPressed;
float currentHorizontalspeed, currentVerticalSpeed, currentApplicateSpeed; // X, Y, and Z
void Awake()
{
playerInputs = new Playerinputs();
characterController = GetComponent<CharacterController>();
playerInputs.PlayerController.move.started += onMoveInput;
playerInputs.PlayerController.move.canceled += onMoveInput;
playerInputs.PlayerController.move.performed += onMoveInput;
playerInputs.PlayerController.Jump.started += onJumpInput;
playerInputs.PlayerController.Jump.canceled += onJumpInput;
}
// So we don't have to redefine those values for every new movement input action
void onMoveInput(InputAction.CallbackContext context)
{
currentInputsMovement = context.ReadValue<Vector2>();
currentMovement.x = currentInputsMovement.x;
currentMovement.z = currentInputsMovement.y;
isMovementPressed = currentMovement.x != 0 || currentMovement.z != 0;
}
void onJumpInput(InputAction.CallbackContext context)
{
isJumpPressed = context.ReadValueAsButton();
}
void Update()
{
Gravity();
Jump();
Walk();
}
#region Walking
[Header("Walking")]
[SerializeField] float _acceleration = 60f;
[SerializeField] float _deAcceleration = 30f;
[SerializeField] float _moveClamp = 13f;
[SerializeField] float apexBonus = 2f;
Vector3 RawMovement;
void Walk()
{
if (currentMovement.x != 0) // X-axis
{
// Set Horizontal speed
currentHorizontalspeed += currentMovement.x * _acceleration * Time.deltaTime;
// Clamp speed
currentHorizontalspeed = Mathf.Clamp(currentHorizontalspeed, -_moveClamp, _moveClamp);
// Apply bonus at the apex of a jump
var apexBonusX = Mathf.Sign(currentMovement.x) * apexBonus * apexPoint;
currentHorizontalspeed += apexBonusX * Time.deltaTime;
}
else // No input, let's slow down the player
{
currentHorizontalspeed = Mathf.MoveTowards(currentHorizontalspeed, 0, _deAcceleration * Time.deltaTime);
}
if (currentMovement.z != 0) // Z-axis
{
currentApplicateSpeed += currentMovement.z * _acceleration * Time.deltaTime;
currentApplicateSpeed = Mathf.Clamp(currentApplicateSpeed, -_moveClamp, _moveClamp);
var apexBonusZ = Mathf.Sign(currentMovement.z) * apexBonus * apexPoint;
currentHorizontalspeed += apexBonusZ * Time.deltaTime;
}
else
{
currentApplicateSpeed = Mathf.MoveTowards(currentApplicateSpeed, 0, _deAcceleration * Time.deltaTime);
}
RawMovement = new Vector3(currentHorizontalspeed, currentVerticalSpeed, currentApplicateSpeed) * Time.deltaTime; // Used externally
characterController.Move(RawMovement);
}
#endregion
#region Jump
[Header("Jump")]
[SerializeField] float JumpHeight = 10f;
[SerializeField] float endedJumpEarlyModifier = 3f;
[SerializeField] float _jumpApexThreshold = 10f;
//[SerializeField] float coyoteTimeThreshold = 1f; // This should be barly have an effect on the player but you may rise the value to check if it works
bool isJumpPressed;
bool endedJumpEarly = false;
float apexPoint;
bool isJumping = false;
bool isGrounded;
//private bool CanUseCoyote => coyoteUsable && !isGrounded && timeLeftGround + coyoteTimeThreshold > Time.time;
void apex()
{
if (!isGrounded)
{
// Gets stronger the closer to the top of the jump
apexPoint = Mathf.InverseLerp(_jumpApexThreshold, 0, Mathf.Abs(characterController.velocity.y));
_fallSpeed = Mathf.Lerp(minGravity, maxGravity, apexPoint);
}
else
{
apexPoint = 0;
}
}
void Jump()
{
apex();
isGrounded = characterController.isGrounded;// The player won't check on what layer it hit. In case you want to add a layer, than add it here
if (isGrounded && isJumpPressed)
{
currentVerticalSpeed = JumpHeight;
endedJumpEarly = false;
isJumping = true;
}
else
{
isJumping = false;
}
// Player left the jump key early
if (!isGrounded && !isJumpPressed && !isJumping && characterController.velocity.y > 0)
{
endedJumpEarly = true;
}
}
#endregion
#region Gravity
[Header("Gravity")]
[SerializeField] float maxGravity = 9.8f;
[SerializeField] float minGravity = 1.8f;
[SerializeField] float groundedGravity = 0.5f;
[SerializeField] float clampfallSpeed = -40f;
float _fallSpeed;
void Gravity()
{
if (isGrounded)
{
currentVerticalSpeed = groundedGravity * Time.deltaTime;
}
else
{
float fallSpeed =
endedJumpEarly ? _fallSpeed * maxGravity * endedJumpEarlyModifier : _fallSpeed * maxGravity;
currentVerticalSpeed -= fallSpeed * Time.deltaTime;
// Limit the fall speed
if (currentVerticalSpeed < clampfallSpeed)
currentVerticalSpeed = clampfallSpeed;
}
}
#endregion
#region OnEnable & OnDisable
private void OnEnable()
{
playerInputs.PlayerController.Enable(); // Enable action map
}
private void OnDisable()
{
playerInputs.PlayerController.Disable(); // Disable action map
}
#endregion
}
}