AnimatorController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using UnityEngine;
  2. public class AnimatorController : MonoBehaviour
  3. {
  4. [Header("Input")]
  5. public float speed;
  6. public bool jump;
  7. [Header("Player")]
  8. public float walkSpeed = 2.0f;
  9. public float runSpeed = 6.0f;
  10. [Range(0f, 1f)]
  11. public float freeLookRange;
  12. public Transform head;
  13. public Transform lookAtTarget;
  14. public Camera playerCamera;
  15. [Space(10)]
  16. [Header("Jump")]
  17. //滞空后开始下落动画时间
  18. public float FallTimeout = 0.15f;
  19. //判断是否接地
  20. [Header("Player Grounded")]
  21. public LayerMask groundLayers;
  22. public float groundedOffset = -0.14f;
  23. public float groundedRadius = 0.28f;
  24. public bool grounded = true;
  25. //脚步声
  26. public AudioClip landingAudioClip;
  27. public AudioClip[] footstepAudioClips;
  28. [Range(0, 1)]
  29. public float footstepAudioVolume = 0.5f;
  30. private Animator _animator;
  31. private bool _hasAnimator;
  32. // animation IDs
  33. private int _animIDSpeed;
  34. private int _animIDGrounded;
  35. private int _animIDJump;
  36. private int _animIDFreeFall;
  37. // 滞空时间
  38. private float _fallTimeoutTimer;
  39. void Start()
  40. {
  41. _animIDSpeed = Animator.StringToHash("Speed");
  42. _animIDGrounded = Animator.StringToHash("Grounded");
  43. _animIDJump = Animator.StringToHash("Jump");
  44. _animIDFreeFall = Animator.StringToHash("FreeFall");
  45. _hasAnimator = TryGetComponent(out _animator);
  46. if (playerCamera == null)
  47. playerCamera = Camera.main;
  48. }
  49. // Update is called once per frame
  50. void FixedUpdate()
  51. {
  52. JumpAndGravity();
  53. GroundedCheck();
  54. Move();
  55. LookAtTarget();
  56. }
  57. private void JumpAndGravity()
  58. {
  59. if (grounded)
  60. {
  61. // 重置滞空时间
  62. _fallTimeoutTimer = FallTimeout;
  63. // 动画设为非空中状态
  64. if (_hasAnimator)
  65. {
  66. _animator.SetBool(_animIDJump, false);
  67. _animator.SetBool(_animIDFreeFall, false);
  68. }
  69. // 满足条件开始跳跃
  70. //Debug.Log(jump + "*********" + _hasAnimator);
  71. if (jump)
  72. {
  73. if (_hasAnimator)
  74. {
  75. _animator.SetBool(_animIDJump, true);
  76. }
  77. }
  78. jump = false;
  79. }
  80. else
  81. {
  82. // 计算滞空时间
  83. if (_fallTimeoutTimer >= 0f)
  84. {
  85. _fallTimeoutTimer -= Time.deltaTime;
  86. //Debug.Log(_fallTimeoutTimer);
  87. }
  88. else
  89. {
  90. // 滞空结束,开始播放下落动画
  91. if (_hasAnimator)
  92. {
  93. if (GroundedHeight() > 0.4f)
  94. _animator.SetBool(_animIDFreeFall, true);
  95. }
  96. }
  97. jump = false;
  98. }
  99. }
  100. float GroundedHeight()
  101. {
  102. RaycastHit hit;
  103. if (Physics.Raycast(transform.position, -transform.up, out hit, 100, groundLayers))
  104. {
  105. return hit.distance;
  106. }
  107. else
  108. {
  109. return 0;
  110. }
  111. }
  112. // 检查指定位置的球体与地面接触,判断是否接地
  113. private void GroundedCheck()
  114. {
  115. Vector3 spherePosition = new Vector3(
  116. transform.position.x,
  117. transform.position.y - groundedOffset,
  118. transform.position.z
  119. );
  120. grounded = Physics.CheckSphere(
  121. spherePosition,
  122. groundedRadius,
  123. groundLayers,
  124. QueryTriggerInteraction.Ignore
  125. );
  126. if (_hasAnimator)
  127. {
  128. _animator.SetBool(_animIDGrounded, grounded);
  129. }
  130. }
  131. private void Move()
  132. {
  133. float animSpeed = 0f;
  134. if (_hasAnimator)
  135. {
  136. if (speed >= 0 && speed <= walkSpeed)
  137. {
  138. animSpeed = RemapNum(speed, 0f, walkSpeed, 0f, 2f);
  139. }
  140. if (speed > walkSpeed)
  141. {
  142. speed = Mathf.Clamp(speed, 0f, 6.0f);
  143. animSpeed = RemapNum(speed, walkSpeed, runSpeed, 2f, 6f);
  144. }
  145. _animator.SetFloat(_animIDSpeed, animSpeed);
  146. }
  147. }
  148. private void LookAtTarget()
  149. {
  150. Vector3 cameraForward = playerCamera.transform.forward;
  151. if (Vector3.Dot(cameraForward, head.forward) < freeLookRange)
  152. {
  153. lookAtTarget.position = Vector3.Lerp(
  154. lookAtTarget.position,
  155. head.position + head.forward,
  156. 0.1f
  157. );
  158. }
  159. else
  160. {
  161. lookAtTarget.position = Vector3.Lerp(
  162. lookAtTarget.position,
  163. head.position + cameraForward,
  164. 0.1f
  165. );
  166. }
  167. }
  168. private float RemapNum(float input, float oldMin, float oldMax, float newMin, float newMax)
  169. {
  170. float result = (newMax - newMin) / (oldMax - oldMin) * (input - oldMin) + newMin;
  171. return result;
  172. }
  173. //球体与地面接触判断触地可视
  174. private void OnDrawGizmosSelected()
  175. {
  176. Color transparentGreen = new Color(0.0f, 1.0f, 0.0f, 0.35f);
  177. Color transparentRed = new Color(1.0f, 0.0f, 0.0f, 0.35f);
  178. if (grounded)
  179. Gizmos.color = transparentGreen;
  180. else
  181. Gizmos.color = transparentRed;
  182. // when selected, draw a gizmo in the position of, and matching radius of, the grounded collider
  183. Gizmos.DrawSphere(
  184. new Vector3(
  185. transform.position.x,
  186. transform.position.y - groundedOffset,
  187. transform.position.z
  188. ),
  189. groundedRadius
  190. );
  191. }
  192. private void OnFootstep(AnimationEvent animationEvent)
  193. {
  194. if (animationEvent.animatorClipInfo.weight > 0.5f)
  195. {
  196. if (footstepAudioClips.Length > 0)
  197. {
  198. var index = Random.Range(0, footstepAudioClips.Length);
  199. AudioSource.PlayClipAtPoint(
  200. footstepAudioClips[index],
  201. transform.transform.position,
  202. footstepAudioVolume
  203. );
  204. }
  205. }
  206. }
  207. private void OnLand(AnimationEvent animationEvent)
  208. {
  209. if (animationEvent.animatorClipInfo.weight > 0.5f)
  210. {
  211. AudioSource.PlayClipAtPoint(landingAudioClip, transform.position, footstepAudioVolume);
  212. }
  213. }
  214. }