MoveController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System.Security.Cryptography;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. public class MoveController : MonoBehaviour
  5. {
  6. public PlayerInputComponent playerInput;
  7. public Transform camera;
  8. public float jumpHeight = 2.0f;
  9. public float gravity = -13.524f;
  10. private int groundLayer = ~(1 << 6);
  11. private float speed = 5f;
  12. private float verticalVelocity = 0.0f;
  13. private float angleX;
  14. private float angleY;
  15. private float jumpInterval = 0.5f;
  16. private float jumpTime = 0.5f;
  17. private bool isGrounded = false;
  18. private bool hasLand = false;
  19. private AnimatorController controller;
  20. private Vector3 move;
  21. private Vector3 gravityMove;
  22. public enum PlayerState
  23. {
  24. Ground,
  25. JumpUp,
  26. JumpDown,
  27. }
  28. private CharacterController character;
  29. public PlayerState state;
  30. // Start is called before the first frame update
  31. void Start()
  32. {
  33. controller = GetComponent<AnimatorController>();
  34. character = GetComponent<CharacterController>();
  35. playerInput=GetComponent<PlayerInputComponent>();
  36. angleX = Vector3.Angle(Vector3.right, camera.right);
  37. angleY = Vector3.Angle(Vector3.up, camera.up);
  38. character.enabled = true;
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. if (StepManager.isFaceBuidling) playerInput.InputReset();
  44. CharacterMovement();
  45. }
  46. /// <summary>
  47. /// 角色移动旋转总逻辑
  48. /// </summary>
  49. void CharacterMovement()
  50. {
  51. if (hasLand)
  52. {
  53. jumpTime += Time.deltaTime;
  54. }
  55. // 检测是否在地面上
  56. isGrounded = IsGrounded();
  57. //Debug.Log(isGrounded);
  58. if (isGrounded)
  59. {
  60. // 按下空格键触发跳跃
  61. if (playerInput.JumpInput)
  62. {
  63. //state = PlayerState.JumpUp;
  64. //Jump();
  65. //controller.jump = true;
  66. //jumpTime = 0;
  67. //hasLand = false;
  68. if (jumpTime >= jumpInterval)
  69. {
  70. state = PlayerState.JumpUp;
  71. Jump();
  72. controller.jump = true;
  73. jumpTime = 0;
  74. hasLand = false;
  75. }
  76. }
  77. }
  78. else
  79. {
  80. if (state != PlayerState.JumpUp)
  81. {
  82. state = PlayerState.JumpDown;
  83. }
  84. }
  85. if (isGrounded && state == PlayerState.JumpDown)
  86. {
  87. state = PlayerState.Ground;
  88. hasLand = true;
  89. }
  90. move = Vector3.zero;
  91. if (state != PlayerState.Ground)
  92. {
  93. // 应用重力
  94. ApplyGravity();
  95. }
  96. //Debug.Log(playerInput.MoveInput);
  97. move +=
  98. new Vector3(playerInput.MoveInput.x, 0, playerInput.MoveInput.y)
  99. * Time.deltaTime
  100. * speed;
  101. // 移动物体
  102. Move();
  103. UpdateControlRotation();
  104. }
  105. void Jump()
  106. {
  107. // 计算跳跃速度以达到指定的跳跃高度
  108. verticalVelocity = Mathf.Sqrt(-2.0f * gravity * jumpHeight);
  109. }
  110. void ApplyGravity()
  111. {
  112. // 应用重力以模拟物体的垂直运动
  113. verticalVelocity += gravity * Time.deltaTime;
  114. // 根据垂直速度移动物体
  115. if (verticalVelocity < 0.1f && verticalVelocity > -0.1f && state == PlayerState.JumpUp)
  116. {
  117. state = PlayerState.JumpDown;
  118. }
  119. gravityMove = new Vector3(0, verticalVelocity * Time.deltaTime, 0);
  120. }
  121. void Move()
  122. {
  123. //Debug.Log(playerInput.name + "****" + move);
  124. //var camRot = new Vector3(0, camera.eulerAngles.y, 0);
  125. if (playerInput.MoveInput != Vector2.zero)
  126. {
  127. Vector3 dir = Vector3.zero;
  128. //Debug.Log(playerInput.MoveInput);
  129. if (playerInput.MoveInput.x > 0)
  130. {
  131. dir = Vector3.ProjectOnPlane(camera.right, Vector3.up);
  132. }
  133. if (playerInput.MoveInput.x < 0)
  134. {
  135. dir += Vector3.ProjectOnPlane(-camera.right, Vector3.up);
  136. }
  137. if (playerInput.MoveInput.y > 0)
  138. {
  139. dir += Vector3.ProjectOnPlane(camera.forward, Vector3.up);
  140. }
  141. if (playerInput.MoveInput.y < 0)
  142. {
  143. dir += Vector3.ProjectOnPlane(-camera.forward, Vector3.up);
  144. }
  145. //Debug.Log(dir);
  146. Vector3 targetDir = Vector3.RotateTowards(
  147. transform.forward,
  148. //camera.forward.ProjectOntoPlane(Vector3.up),
  149. dir,
  150. 10,
  151. 0f
  152. );
  153. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDir),Time.deltaTime * 15f);
  154. //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
  155. //Quaternion r = Quaternion.Euler(0, a, 0);
  156. //transform.eulerAngles = r * transform.eulerAngles;
  157. }
  158. //var m = move.magnitude * camera.forward + gravityMove;
  159. //Debug.Log(move + "*******" + "************" + m);
  160. var angle = AngleSigned(
  161. Vector3.forward,
  162. Vector3.ProjectOnPlane(camera.forward, Vector3.up),
  163. transform.up
  164. );
  165. Quaternion rot = Quaternion.Euler(0, angle, 0);
  166. //Vector3 move2 = camera.transform.TransformDirection(move);
  167. controller.speed = move.magnitude * 300;
  168. character.Move(rot * move + gravityMove);
  169. camera.position= character.transform.position+new Vector3(0,1.148f,0);
  170. }
  171. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
  172. {
  173. return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))
  174. * Mathf.Rad2Deg;
  175. }
  176. private Vector3 pointBottom;
  177. private Vector3 pointTop;
  178. private float radius = 0.5f;
  179. private Collider[] colliders;
  180. private float LerpValue = 0.05f;
  181. bool IsGrounded()
  182. {
  183. // 在这里你可以添加检测物体是否在地面上的逻辑
  184. // 例如射线检测、碰撞器检测等
  185. radius = character.radius * 0.9f;
  186. pointBottom = transform.position + transform.up * radius - transform.up * 0.1f;
  187. pointTop = transform.position + transform.up * character.height - transform.up * radius;
  188. colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, groundLayer);
  189. Debug.DrawLine(pointBottom, pointTop, Color.red);
  190. if (colliders.Length != 0)
  191. {
  192. return true;
  193. }
  194. else
  195. {
  196. return false;
  197. }
  198. }
  199. private void UpdateControlRotation()
  200. {
  201. if (playerInput.touch0Input != Vector2.zero && playerInput.MoveInput == Vector2.zero && !EventSystem.current.IsPointerOverGameObject())
  202. {
  203. //camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
  204. angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
  205. angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
  206. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  207. //使用插值平滑旋转
  208. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  209. }
  210. if (playerInput.touch1Input != Vector2.zero && !EventSystem.current.IsPointerOverGameObject())
  211. {
  212. angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
  213. angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
  214. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  215. //使用插值平滑旋转
  216. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  217. }
  218. angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
  219. angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
  220. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  221. //使用插值平滑旋转
  222. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  223. }
  224. private static float angleClamp(float angle, float min, float max)
  225. {//一个限制角度最大最小值的方法
  226. if (angle < -360)
  227. angle += 360;
  228. if (angle > 360)
  229. angle -= 360;
  230. return Mathf.Clamp(angle, min, max);
  231. }
  232. }