Advertisement
Guest User

Invector

a guest
Aug 31st, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace Invector.vCharacterController
  4. {
  5. public class vThirdPersonController : vThirdPersonAnimator
  6. {
  7. public virtual void ControlAnimatorRootMotion()
  8. {
  9. if (!this.enabled) return;
  10.  
  11. if (inputSmooth == Vector3.zero)
  12. {
  13. transform.position = animator.rootPosition;
  14. transform.rotation = animator.rootRotation;
  15. }
  16.  
  17. if (useRootMotion)
  18. MoveCharacter(moveDirection);
  19. }
  20.  
  21. public virtual void ControlLocomotionType()
  22. {
  23. if (lockMovement) return;
  24.  
  25. if (locomotionType.Equals(LocomotionType.FreeWithStrafe) && !isStrafing || locomotionType.Equals(LocomotionType.OnlyFree))
  26. {
  27. SetControllerMoveSpeed(freeSpeed);
  28. SetAnimatorMoveSpeed(freeSpeed);
  29. }
  30. else if (locomotionType.Equals(LocomotionType.OnlyStrafe) || locomotionType.Equals(LocomotionType.FreeWithStrafe) && isStrafing)
  31. {
  32. isStrafing = true;
  33. SetControllerMoveSpeed(strafeSpeed);
  34. SetAnimatorMoveSpeed(strafeSpeed);
  35. }
  36.  
  37. if (!useRootMotion)
  38. MoveCharacter(moveDirection);
  39. }
  40.  
  41. public virtual void ControlRotationType()
  42. {
  43. if (lockRotation) return;
  44.  
  45. bool validInput = input != Vector3.zero || (isStrafing ? strafeSpeed.rotateWithCamera : freeSpeed.rotateWithCamera);
  46.  
  47. if (validInput)
  48. {
  49. // calculate input smooth
  50. inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
  51.  
  52. Vector3 dir = (isStrafing && (!isSprinting || sprintOnlyFree == false) || (freeSpeed.rotateWithCamera && input == Vector3.zero)) && rotateTarget ? rotateTarget.forward : moveDirection;
  53. RotateToDirection(dir);
  54. }
  55. }
  56.  
  57. public virtual void UpdateMoveDirection(Transform referenceTransform = null)
  58. {
  59. if (input.magnitude <= 0.01)
  60. {
  61. moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
  62. return;
  63. }
  64.  
  65. if (referenceTransform && !rotateByWorld)
  66. {
  67. //get the right-facing direction of the referenceTransform
  68. var right = referenceTransform.right;
  69. right.y = 0;
  70. //get the forward direction relative to referenceTransform Right
  71. var forward = Quaternion.AngleAxis(-90, Vector3.up) * right;
  72. // determine the direction the player will face based on input and the referenceTransform's right and forward directions
  73. moveDirection = (inputSmooth.x * right) + (inputSmooth.z * forward);
  74. }
  75. else
  76. {
  77. moveDirection = new Vector3(inputSmooth.x, 0, inputSmooth.z);
  78. }
  79. }
  80.  
  81. public virtual void Sprint(bool value)
  82. {
  83. var sprintConditions = (input.sqrMagnitude > 0.1f && isGrounded &&
  84. !(isStrafing && !strafeSpeed.walkByDefault && (horizontalSpeed >= 0.5 || horizontalSpeed <= -0.5 || verticalSpeed <= 0.1f)));
  85.  
  86. if (value && sprintConditions)
  87. {
  88. if (input.sqrMagnitude > 0.1f)
  89. {
  90. if (isGrounded && useContinuousSprint)
  91. {
  92. isSprinting = !isSprinting;
  93. }
  94. else if (!isSprinting)
  95. {
  96. isSprinting = true;
  97. }
  98. }
  99. else if (!useContinuousSprint && isSprinting)
  100. {
  101. isSprinting = false;
  102. }
  103. }
  104. else if (isSprinting)
  105. {
  106. isSprinting = false;
  107. }
  108. }
  109.  
  110. public virtual void Strafe()
  111. {
  112. isStrafing = !isStrafing;
  113. }
  114.  
  115. public virtual void Jump()
  116. {
  117. // trigger jump behaviour
  118. jumpCounter = jumpTimer;
  119. isJumping = true;
  120.  
  121. // trigger jump animations
  122. if (input.sqrMagnitude < 0.1f)
  123. animator.CrossFadeInFixedTime("Jump", 0.1f);
  124. else
  125. animator.CrossFadeInFixedTime("JumpMove", .2f);
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement