-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerCharacterController.cs
More file actions
627 lines (548 loc) · 28.8 KB
/
Copy pathPlayerCharacterController.cs
File metadata and controls
627 lines (548 loc) · 28.8 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KinematicCharacterController;
using System;
/*
* Changelog
* 3/10 - Supports movement using controller
* 2/23 - Forbids dashing when in mid air
*/
public enum CharacterState
{
Default,
}
public enum OrientationMethod
{
TowardsCamera,
TowardsMovement,
TowardsMouse,
UseRightStick
}
public struct PlayerCharacterInputs
{
public float MoveAxisForward;
public float MoveAxisRight;
public float LookAxisForward;
public float LookAxisRight;
public bool JumpDown;
public bool Dash;
//TODO deprecate...
public Quaternion CameraRotation;
public bool CrouchDown;
public bool CrouchUp;
}
public struct AICharacterInputs
{
public Vector3 MoveVector;
public Vector3 LookVector;
}
public enum BonusOrientationMethod
{
None,
TowardsGravity,
TowardsGroundSlopeAndGravity,
}
public class PlayerCharacterController : MonoBehaviour, ICharacterController
{
[Tooltip("An object that is placed along the ground where the mouse is. " +
"Player looks towards this object, give it a renderer to debug look issues. Also used for aimed shots.")]
public GameObject lookTargetPoint;
public Camera worldSpaceCamera;
public KinematicCharacterMotor Motor;
private PlayerSettings PlayerSettings;
public GenericSkillCooldown dashCooldownManager;
[Header("Stable Movement")]
public float MaxStableMoveSpeedCurrent = 10f;
public float MaxStableMoveSpeedNormal = 10f;
public float MaxStableMoveSpeedDashing = 50f;
public float StableMovementSharpness = 15f;
public float OrientationSharpness = 10f;
public OrientationMethod OrientationMethod = OrientationMethod.TowardsCamera;
[Header("Air Movement")]
public float MaxAirMoveSpeed = 15f;
public float AirAccelerationSpeed = 15f;
public float Drag = 0.1f;
[Header("Jumping")]
public bool AllowJumpingWhenSliding = false;
public float JumpUpSpeed = 10f;
public float JumpScalableForwardSpeed = 10f;
public float JumpPreGroundingGraceTime = 0f;
public float JumpPostGroundingGraceTime = 0f;
[Header("Misc")]
public List<Collider> IgnoredColliders = new List<Collider>();
public BonusOrientationMethod BonusOrientationMethod = BonusOrientationMethod.None;
public float BonusOrientationSharpness = 10f;
public Vector3 Gravity = new Vector3(0, -30f, 0);
public Transform MeshRoot;
public Transform CameraFollowPoint;
public float CrouchedCapsuleHeight = 1f;
public CharacterState CurrentCharacterState { get; private set; }
private Collider[] _probedColliders = new Collider[8];
private RaycastHit[] _probedHits = new RaycastHit[8];
private Vector3 _moveInputVector;
private Vector3 _lookInputVector;
private bool _jumpRequested = false;
private bool _dashRequested = false;
private bool _jumpConsumed = false;
private bool _jumpedThisFrame = false;
private bool _dashing = false;
private float _nextDashReady = 0f;
private float _currentDashEnds = 0f;
private int _dashesAvailable = 0;
private float _timeSinceJumpRequested = Mathf.Infinity;
private float _timeSinceLastAbleToJump = 0f;
private Vector3 _internalVelocityAdd = Vector3.zero;
private bool _shouldBeCrouching = false;
private bool _isCrouching = false;
private Vector3 lastInnerNormal = Vector3.zero;
private Vector3 lastOuterNormal = Vector3.zero;
private void Awake()
{
// Handle initial state
TransitionToState(CharacterState.Default);
// Assign the characterController to the motor
Motor.CharacterController = this;
//Link to the playerSettings and set up dash cooldown
PlayerSettings = PlayerSettings.getInstance();
dashCooldownManager.updateSkillCooldown(Time.time, _nextDashReady, _dashesAvailable, PlayerSettings.MaxDashCharges);
}
/// <summary>
/// Handles movement state transitions and enter/exit callbacks
/// </summary>
public void TransitionToState(CharacterState newState)
{
CharacterState tmpInitialState = CurrentCharacterState;
OnStateExit(tmpInitialState, newState);
CurrentCharacterState = newState;
OnStateEnter(newState, tmpInitialState);
}
/// <summary>
/// Event when entering a state
/// </summary>
public void OnStateEnter(CharacterState state, CharacterState fromState)
{
switch (state)
{
case CharacterState.Default:
{
break;
}
}
}
/// <summary>
/// Event when exiting a state
/// </summary>
public void OnStateExit(CharacterState state, CharacterState toState)
{
switch (state)
{
case CharacterState.Default:
{
break;
}
}
}
/// <summary>
/// This is called every frame by ExamplePlayer in order to tell the character what its inputs are
/// </summary>
public void SetInputs(ref PlayerCharacterInputs inputs)
{
// Clamp input
Vector3 moveInputVector = Vector3.ClampMagnitude(new Vector3(inputs.MoveAxisRight, 0f, inputs.MoveAxisForward), 1f);
// Calculate camera direction and rotation on the character plane
Vector3 cameraPlanarDirection = Vector3.ProjectOnPlane(inputs.CameraRotation * Vector3.forward, Motor.CharacterUp).normalized;
if (cameraPlanarDirection.sqrMagnitude == 0f)
{
cameraPlanarDirection = Vector3.ProjectOnPlane(inputs.CameraRotation * Vector3.up, Motor.CharacterUp).normalized;
}
Quaternion cameraPlanarRotation = Quaternion.LookRotation(cameraPlanarDirection, Motor.CharacterUp);
switch (CurrentCharacterState)
{
case CharacterState.Default:
{
// Move and look inputs
_moveInputVector = cameraPlanarRotation * moveInputVector;
switch (OrientationMethod)
{
case OrientationMethod.TowardsCamera:
_lookInputVector = cameraPlanarDirection;
break;
case OrientationMethod.TowardsMovement:
_lookInputVector = _moveInputVector.normalized;
break;
case OrientationMethod.TowardsMouse:
Vector3 lookAt = Vector3.zero;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
int layerMask = LayerMask.GetMask("PlayerRotationTarget");
if(Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) {
Debug.DrawLine(ray.origin, hit.point);
Vector3 hitSpot = hit.point;
if (lookTargetPoint != null) {
lookTargetPoint.transform.position = hitSpot;
}
if (hit.transform != null && hit.transform.CompareTag("Player")) {
// Debug.Log("hit player");
lookAt = Vector3.zero;
} else {
lookAt = (hitSpot - transform.position).normalized;
}
}
_lookInputVector = lookAt;
/* // V1 DECENT FOR DIRECT OVERHEAD CAMERAS
Vector3 mouse = Input.mousePosition;
Vector3 mouseToWorld = worldSpaceCamera.ScreenToWorldPoint(new Vector3(mouse.x, mouse.y, worldSpaceCamera.transform.position.y));
// mouseToWorld = new Vector3(mouseToWorld.x, transform.position.y, mouseToWorld.z);
Debug.Log(mouseToWorld);
if(debugTestingCube != null) {
debugTestingCube.transform.position = mouseToWorld;
}
Vector3 lookAt = (mouseToWorld - transform.position).normalized;
_lookInputVector = lookAt;
*/
break;
case OrientationMethod.UseRightStick:
Vector3 lookTarget = transform.position + (Vector3.forward * inputs.LookAxisForward + Vector3.right * inputs.LookAxisRight);
Vector3 lookAtNormed = (lookTarget - transform.position).normalized;
if (lookTargetPoint != null) {
lookTargetPoint.transform.position = lookTarget;
}
_lookInputVector = lookAtNormed;
break;
}
// Jumping input
if (inputs.JumpDown)
{
_timeSinceJumpRequested = 0f;
_jumpRequested = true;
}
if (inputs.Dash) {
_dashRequested = true;
}
// Crouching input
if (inputs.CrouchDown)
{
_shouldBeCrouching = true;
if (!_isCrouching)
{
_isCrouching = true;
Motor.SetCapsuleDimensions(0.5f, CrouchedCapsuleHeight, CrouchedCapsuleHeight * 0.5f);
MeshRoot.localScale = new Vector3(1f, 0.5f, 1f);
}
}
else if (inputs.CrouchUp)
{
_shouldBeCrouching = false;
}
break;
}
}
}
/// <summary>
/// This is called every frame by the AI script in order to tell the character what its inputs are
/// </summary>
public void SetInputs(ref AICharacterInputs inputs)
{
_moveInputVector = inputs.MoveVector;
_lookInputVector = inputs.LookVector;
}
private Quaternion _tmpTransientRot;
/// <summary>
/// (Called by KinematicCharacterMotor during its update cycle)
/// This is called before the character begins its movement update
/// </summary>
public void BeforeCharacterUpdate(float deltaTime)
{
}
/// <summary>
/// (Called by KinematicCharacterMotor during its update cycle)
/// This is where you tell your character what its rotation should be right now.
/// This is the ONLY place where you should set the character's rotation
/// </summary>
public void UpdateRotation(ref Quaternion currentRotation, float deltaTime)
{
switch (CurrentCharacterState)
{
case CharacterState.Default:
{
if (_lookInputVector.sqrMagnitude > 0f && OrientationSharpness > 0f)
{
// Smoothly interpolate from current to target look direction
Vector3 smoothedLookInputDirection = Vector3.Slerp(Motor.CharacterForward, _lookInputVector, 1 - Mathf.Exp(-OrientationSharpness * deltaTime)).normalized;
// Set the current rotation (which will be used by the KinematicCharacterMotor)
currentRotation = Quaternion.LookRotation(smoothedLookInputDirection, Motor.CharacterUp);
}
Vector3 currentUp = (currentRotation * Vector3.up);
if (BonusOrientationMethod == BonusOrientationMethod.TowardsGravity)
{
// Rotate from current up to invert gravity
Vector3 smoothedGravityDir = Vector3.Slerp(currentUp, -Gravity.normalized, 1 - Mathf.Exp(-BonusOrientationSharpness * deltaTime));
currentRotation = Quaternion.FromToRotation(currentUp, smoothedGravityDir) * currentRotation;
}
else if (BonusOrientationMethod == BonusOrientationMethod.TowardsGroundSlopeAndGravity)
{
if (Motor.GroundingStatus.IsStableOnGround)
{
Vector3 initialCharacterBottomHemiCenter = Motor.TransientPosition + (currentUp * Motor.Capsule.radius);
Vector3 smoothedGroundNormal = Vector3.Slerp(Motor.CharacterUp, Motor.GroundingStatus.GroundNormal, 1 - Mathf.Exp(-BonusOrientationSharpness * deltaTime));
currentRotation = Quaternion.FromToRotation(currentUp, smoothedGroundNormal) * currentRotation;
// Move the position to create a rotation around the bottom hemi center instead of around the pivot
Motor.SetTransientPosition(initialCharacterBottomHemiCenter + (currentRotation * Vector3.down * Motor.Capsule.radius));
}
else
{
Vector3 smoothedGravityDir = Vector3.Slerp(currentUp, -Gravity.normalized, 1 - Mathf.Exp(-BonusOrientationSharpness * deltaTime));
currentRotation = Quaternion.FromToRotation(currentUp, smoothedGravityDir) * currentRotation;
}
}
else
{
Vector3 smoothedGravityDir = Vector3.Slerp(currentUp, Vector3.up, 1 - Mathf.Exp(-BonusOrientationSharpness * deltaTime));
currentRotation = Quaternion.FromToRotation(currentUp, smoothedGravityDir) * currentRotation;
}
break;
}
}
}
/// <summary>
/// (Called by KinematicCharacterMotor during its update cycle)
/// This is where you tell your character what its velocity should be right now.
/// This is the ONLY place where you can set the character's velocity
/// </summary>
public void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime)
{
switch (CurrentCharacterState)
{
case CharacterState.Default:
{
// Ground movement
if (Motor.GroundingStatus.IsStableOnGround)
{
float currentVelocityMagnitude = currentVelocity.magnitude;
Vector3 effectiveGroundNormal = Motor.GroundingStatus.GroundNormal;
// Reorient velocity on slope
currentVelocity = Motor.GetDirectionTangentToSurface(currentVelocity, effectiveGroundNormal) * currentVelocityMagnitude;
// Calculate target velocity
Vector3 inputRight = Vector3.Cross(_moveInputVector, Motor.CharacterUp);
Vector3 reorientedInput = Vector3.Cross(effectiveGroundNormal, inputRight).normalized * _moveInputVector.magnitude;
Vector3 targetMovementVelocity = reorientedInput * MaxStableMoveSpeedCurrent;
// Smooth movement Velocity
currentVelocity = Vector3.Lerp(currentVelocity, targetMovementVelocity, 1f - Mathf.Exp(-StableMovementSharpness * deltaTime));
}
// Air movement
else
{
// Add move input
if (_moveInputVector.sqrMagnitude > 0f)
{
Vector3 addedVelocity = _moveInputVector * AirAccelerationSpeed * deltaTime;
Vector3 currentVelocityOnInputsPlane = Vector3.ProjectOnPlane(currentVelocity, Motor.CharacterUp);
// Limit air velocity from inputs
if (currentVelocityOnInputsPlane.magnitude < MaxAirMoveSpeed)
{
// clamp addedVel to make total vel not exceed max vel on inputs plane
Vector3 newTotal = Vector3.ClampMagnitude(currentVelocityOnInputsPlane + addedVelocity, MaxAirMoveSpeed);
addedVelocity = newTotal - currentVelocityOnInputsPlane;
}
else
{
// Make sure added vel doesn't go in the direction of the already-exceeding velocity
if (Vector3.Dot(currentVelocityOnInputsPlane, addedVelocity) > 0f)
{
addedVelocity = Vector3.ProjectOnPlane(addedVelocity, currentVelocityOnInputsPlane.normalized);
}
}
// Prevent air-climbing sloped walls
if (Motor.GroundingStatus.FoundAnyGround)
{
if (Vector3.Dot(currentVelocity + addedVelocity, addedVelocity) > 0f)
{
Vector3 perpenticularObstructionNormal = Vector3.Cross(Vector3.Cross(Motor.CharacterUp, Motor.GroundingStatus.GroundNormal), Motor.CharacterUp).normalized;
addedVelocity = Vector3.ProjectOnPlane(addedVelocity, perpenticularObstructionNormal);
}
}
// Apply added velocity
currentVelocity += addedVelocity;
}
// Gravity
currentVelocity += Gravity * deltaTime;
// Drag
currentVelocity *= (1f / (1f + (Drag * deltaTime)));
}
// Handle jumping
_jumpedThisFrame = false;
_timeSinceJumpRequested += deltaTime;
if (_jumpRequested)
{
// See if we actually are allowed to jump
if (!_jumpConsumed && ((AllowJumpingWhenSliding ? Motor.GroundingStatus.FoundAnyGround : Motor.GroundingStatus.IsStableOnGround) || _timeSinceLastAbleToJump <= JumpPostGroundingGraceTime))
{
// Calculate jump direction before ungrounding
Vector3 jumpDirection = Motor.CharacterUp;
if (Motor.GroundingStatus.FoundAnyGround && !Motor.GroundingStatus.IsStableOnGround)
{
jumpDirection = Motor.GroundingStatus.GroundNormal;
}
// Makes the character skip ground probing/snapping on its next update.
// If this line weren't here, the character would remain snapped to the ground when trying to jump. Try commenting this line out and see.
Motor.ForceUnground();
// Add to the return velocity and reset jump state
currentVelocity += (jumpDirection * JumpUpSpeed) - Vector3.Project(currentVelocity, Motor.CharacterUp);
currentVelocity += (_moveInputVector * JumpScalableForwardSpeed);
_jumpRequested = false;
_jumpConsumed = true;
_jumpedThisFrame = true;
}
}
//Handle dashing
//Can't earn dashes while at max dashes
if(_dashesAvailable == PlayerSettings.MaxDashCharges) {
_nextDashReady = Time.time + PlayerSettings.DashCooldown;
}
//If not at max dashes and time has elapsed, earn a charge
if (Time.time > _nextDashReady && _dashesAvailable < PlayerSettings.MaxDashCharges) {
_dashesAvailable += 1;
_nextDashReady = Time.time + PlayerSettings.DashCooldown;
//Update the UI listener
dashCooldownManager.updateSkillCooldown(Time.time, _nextDashReady, _dashesAvailable, PlayerSettings.MaxDashCharges);
}
//Dash requested, not currently dashing, has dashes available, on stable ground, and moving in some direction: DASH
if (_dashRequested && !_dashing && _dashesAvailable > 0 && Motor.GroundingStatus.IsStableOnGround &&
currentVelocity.sqrMagnitude > .1f) {
_dashing = true;
_dashesAvailable -= 1;
_currentDashEnds = Time.time + PlayerSettings.DashDuration;
MaxStableMoveSpeedCurrent = MaxStableMoveSpeedDashing;
//Update the UI listener
dashCooldownManager.updateSkillCooldown(Time.time, _nextDashReady, _dashesAvailable, PlayerSettings.MaxDashCharges);
}
//Dash input is always locked for only one frame
_dashRequested = false;
//If the dash has ended, reset movement back to normal
if(Time.time > _currentDashEnds) {
_dashing = false;
MaxStableMoveSpeedCurrent = MaxStableMoveSpeedNormal;
}
// Take into account additive velocity
if (_internalVelocityAdd.sqrMagnitude > 0f)
{
currentVelocity += _internalVelocityAdd;
_internalVelocityAdd = Vector3.zero;
}
break;
}
}
}
/// <summary>
/// (Called by KinematicCharacterMotor during its update cycle)
/// This is called after the character has finished its movement update
/// </summary>
public void AfterCharacterUpdate(float deltaTime)
{
switch (CurrentCharacterState)
{
case CharacterState.Default:
{
// Handle jump-related values
{
// Handle jumping pre-ground grace period
if (_jumpRequested && _timeSinceJumpRequested > JumpPreGroundingGraceTime)
{
_jumpRequested = false;
}
if (AllowJumpingWhenSliding ? Motor.GroundingStatus.FoundAnyGround : Motor.GroundingStatus.IsStableOnGround)
{
// If we're on a ground surface, reset jumping values
if (!_jumpedThisFrame)
{
_jumpConsumed = false;
}
_timeSinceLastAbleToJump = 0f;
}
else
{
// Keep track of time since we were last able to jump (for grace period)
_timeSinceLastAbleToJump += deltaTime;
}
}
// Handle uncrouching
if (_isCrouching && !_shouldBeCrouching)
{
// Do an overlap test with the character's standing height to see if there are any obstructions
Motor.SetCapsuleDimensions(0.5f, 2f, 1f);
if (Motor.CharacterOverlap(
Motor.TransientPosition,
Motor.TransientRotation,
_probedColliders,
Motor.CollidableLayers,
QueryTriggerInteraction.Ignore) > 0)
{
// If obstructions, just stick to crouching dimensions
Motor.SetCapsuleDimensions(0.5f, CrouchedCapsuleHeight, CrouchedCapsuleHeight * 0.5f);
}
else
{
// If no obstructions, uncrouch
MeshRoot.localScale = new Vector3(1f, 1f, 1f);
_isCrouching = false;
}
}
break;
}
}
}
public void PostGroundingUpdate(float deltaTime)
{
// Handle landing and leaving ground
if (Motor.GroundingStatus.IsStableOnGround && !Motor.LastGroundingStatus.IsStableOnGround)
{
OnLanded();
}
else if (!Motor.GroundingStatus.IsStableOnGround && Motor.LastGroundingStatus.IsStableOnGround)
{
OnLeaveStableGround();
}
}
public bool IsColliderValidForCollisions(Collider coll)
{
if (IgnoredColliders.Count == 0)
{
return true;
}
if (IgnoredColliders.Contains(coll))
{
return false;
}
return true;
}
public void OnGroundHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport)
{
}
public void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, ref HitStabilityReport hitStabilityReport)
{
}
public void AddVelocity(Vector3 velocity)
{
switch (CurrentCharacterState)
{
case CharacterState.Default:
{
_internalVelocityAdd += velocity;
break;
}
}
}
public void ProcessHitStabilityReport(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, Vector3 atCharacterPosition, Quaternion atCharacterRotation, ref HitStabilityReport hitStabilityReport)
{
}
protected void OnLanded()
{
}
protected void OnLeaveStableGround()
{
}
public void OnDiscreteCollisionDetected(Collider hitCollider)
{
}
}