MoveController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (isGrounded && state == PlayerState.JumpDown)
  58. {
  59. state = PlayerState.Ground;
  60. hasLand = true;
  61. }
  62. //Debug.Log(isGrounded);
  63. if (state == PlayerState.Ground)
  64. {
  65. // 按下空格键触发跳跃
  66. if (playerInput.JumpInput)
  67. {
  68. //state = PlayerState.JumpUp;
  69. //Jump();
  70. //controller.jump = true;
  71. //jumpTime = 0;
  72. //hasLand = false;
  73. if (jumpTime >= jumpInterval)
  74. {
  75. state = PlayerState.JumpUp;
  76. Jump();
  77. controller.jump = true;
  78. jumpTime = 0;
  79. hasLand = false;
  80. }
  81. }
  82. }
  83. else
  84. {
  85. if (state != PlayerState.JumpUp)
  86. {
  87. state = PlayerState.JumpDown;
  88. }
  89. }
  90. move = Vector3.zero;
  91. if (state != PlayerState.Ground)
  92. {
  93. // 应用重力
  94. ApplyGravity();
  95. }
  96. else
  97. {
  98. gravityMove = new Vector3(0, 0, 0);
  99. }
  100. //Debug.Log(playerInput.MoveInput);
  101. move +=
  102. new Vector3(playerInput.MoveInput.x, 0, playerInput.MoveInput.y)
  103. * Time.deltaTime
  104. * speed;
  105. // 移动物体
  106. Move();
  107. UpdateControlRotation();
  108. }
  109. void Jump()
  110. {
  111. // 计算跳跃速度以达到指定的跳跃高度
  112. verticalVelocity = Mathf.Sqrt(-2.0f * gravity * jumpHeight);
  113. //Debug.Log(verticalVelocity);
  114. }
  115. void ApplyGravity()
  116. {
  117. // 应用重力以模拟物体的垂直运动
  118. verticalVelocity += gravity * Time.deltaTime;
  119. // 根据垂直速度移动物体
  120. if (verticalVelocity < 0.1f && verticalVelocity > -0.1f && state == PlayerState.JumpUp)
  121. {
  122. //Debug.Log("*************");
  123. state = PlayerState.JumpDown;
  124. }
  125. gravityMove = new Vector3(0, verticalVelocity * Time.deltaTime, 0);
  126. }
  127. void Move()
  128. {
  129. //Debug.Log(playerInput.name + "****" + move);
  130. //var camRot = new Vector3(0, camera.eulerAngles.y, 0);
  131. if (playerInput.MoveInput != Vector2.zero)
  132. {
  133. Vector3 dir = Vector3.zero;
  134. //Debug.Log(playerInput.MoveInput);
  135. if (playerInput.MoveInput.x > 0)
  136. {
  137. dir = Vector3.ProjectOnPlane(camera.right, Vector3.up);
  138. }
  139. if (playerInput.MoveInput.x < 0)
  140. {
  141. dir += Vector3.ProjectOnPlane(-camera.right, Vector3.up);
  142. }
  143. if (playerInput.MoveInput.y > 0)
  144. {
  145. dir += Vector3.ProjectOnPlane(camera.forward, Vector3.up);
  146. }
  147. if (playerInput.MoveInput.y < 0)
  148. {
  149. dir += Vector3.ProjectOnPlane(-camera.forward, Vector3.up);
  150. }
  151. //Debug.Log(dir);
  152. Vector3 targetDir = Vector3.RotateTowards(
  153. transform.forward,
  154. //camera.forward.ProjectOntoPlane(Vector3.up),
  155. dir,
  156. 10,
  157. 0f
  158. );
  159. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDir),Time.deltaTime * 15f);
  160. //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
  161. //Quaternion r = Quaternion.Euler(0, a, 0);
  162. //transform.eulerAngles = r * transform.eulerAngles;
  163. }
  164. //var m = move.magnitude * camera.forward + gravityMove;
  165. //Debug.Log(move + "*******" + "************" + m);
  166. var angle = AngleSigned(
  167. Vector3.forward,
  168. Vector3.ProjectOnPlane(camera.forward, Vector3.up),
  169. transform.up
  170. );
  171. Quaternion rot = Quaternion.Euler(0, angle, 0);
  172. //Vector3 move2 = camera.transform.TransformDirection(move);
  173. controller.speed = move.magnitude * 300;
  174. //Debug.Log(move.magnitude + "***********" + gravityMove.magnitude);
  175. character.Move(rot * move + gravityMove);
  176. camera.position= character.transform.position+new Vector3(0,1.148f,0);
  177. }
  178. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
  179. {
  180. return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))
  181. * Mathf.Rad2Deg;
  182. }
  183. private Vector3 pointBottom;
  184. private Vector3 pointTop;
  185. private float radius = 0.5f;
  186. private Collider[] colliders;
  187. private float LerpValue = 0.05f;
  188. bool IsGrounded()
  189. {
  190. // 在这里你可以添加检测物体是否在地面上的逻辑
  191. // 例如射线检测、碰撞器检测等
  192. radius = character.radius * 0.9f;
  193. pointBottom = transform.position + transform.up * radius - transform.up * 0.1f;
  194. pointTop = transform.position + transform.up * character.height - transform.up * radius;
  195. colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, groundLayer);
  196. Debug.DrawLine(pointBottom, pointTop, Color.red);
  197. if (colliders.Length != 0)
  198. {
  199. return true;
  200. }
  201. else
  202. {
  203. return false;
  204. }
  205. }
  206. private void UpdateControlRotation()
  207. {
  208. if (playerInput.touch0Input != Vector2.zero && playerInput.MoveInput == Vector2.zero && !EventSystem.current.IsPointerOverGameObject())
  209. {
  210. //camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
  211. angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
  212. angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
  213. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  214. //使用插值平滑旋转
  215. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  216. }
  217. if (playerInput.touch1Input != Vector2.zero && !EventSystem.current.IsPointerOverGameObject())
  218. {
  219. angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
  220. angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
  221. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  222. //使用插值平滑旋转
  223. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  224. }
  225. angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
  226. angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
  227. angleY = angleClamp(angleY, -1, 60);//相机旋转角度限制
  228. //使用插值平滑旋转
  229. camera.rotation = Quaternion.Lerp(camera.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 5);
  230. }
  231. private static float angleClamp(float angle, float min, float max)
  232. {//一个限制角度最大最小值的方法
  233. if (angle < -360)
  234. angle += 360;
  235. if (angle > 360)
  236. angle -= 360;
  237. return Mathf.Clamp(angle, min, max);
  238. }
  239. }