MoveController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using static UnityEditor.ShaderGraph.Internal.KeywordDependentCollection;
  4. public class MoveController : MonoBehaviour
  5. {
  6. public PlayerInputComponent playerInput;
  7. public Transform camRoot;
  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 bool isBlock = false;
  20. private AnimatorController controller;
  21. private Transform head;
  22. private Transform m_camera;
  23. private Vector3 move;
  24. private Vector3 gravityMove;
  25. private Vector3 lastMove;
  26. private Vector3 camInitPos;
  27. private RaycastHit hit;
  28. private Vector3 colliderDir;
  29. public enum PlayerState
  30. {
  31. Ground,
  32. JumpUp,
  33. JumpDown,
  34. }
  35. private CharacterController character;
  36. public PlayerState state;
  37. // Start is called before the first frame update
  38. void Start()
  39. {
  40. controller = GetComponent<AnimatorController>();
  41. character = GetComponent<CharacterController>();
  42. playerInput=GetComponent<PlayerInputComponent>();
  43. angleX = Vector3.Angle(Vector3.right, camRoot.right);
  44. angleY = Vector3.Angle(Vector3.up, camRoot.up);
  45. character.enabled = true;
  46. head = transform.Find("Head");
  47. m_camera = camRoot.transform.Find("Main Camera");
  48. camInitPos = m_camera.localPosition;
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. if (StepManager.isFaceBuidling)
  54. playerInput.InputReset();
  55. CharacterMovement();
  56. }
  57. /// <summary>
  58. /// ��ɫ�ƶ���ת���߼�
  59. /// </summary>
  60. void CharacterMovement()
  61. {
  62. if (hasLand)
  63. {
  64. jumpTime += Time.deltaTime;
  65. }
  66. // ����Ƿ��ڵ�����
  67. //isGrounded = IsGrounded();
  68. isGrounded = IsGrounded();
  69. if (isGrounded && state == PlayerState.JumpDown)
  70. {
  71. verticalVelocity = 0;
  72. state = PlayerState.Ground;
  73. hasLand = true;
  74. }
  75. //Debug.Log(isGrounded);
  76. if (state == PlayerState.Ground)
  77. {
  78. // ���¿ո��������Ծ
  79. if (playerInput.JumpInput)
  80. {
  81. //state = PlayerState.JumpUp;
  82. //Jump();
  83. //controller.jump = true;
  84. //jumpTime = 0;
  85. //hasLand = false;
  86. if (jumpTime >= jumpInterval)
  87. {
  88. state = PlayerState.JumpUp;
  89. Jump();
  90. controller.jump = true;
  91. jumpTime = 0;
  92. hasLand = false;
  93. }
  94. }
  95. }
  96. if (!isGrounded && state != PlayerState.JumpUp)
  97. {
  98. state = PlayerState.JumpDown;
  99. }
  100. move = Vector3.zero;
  101. if (state != PlayerState.Ground)
  102. {
  103. // Ӧ������
  104. ApplyGravity();
  105. }
  106. else
  107. {
  108. gravityMove = new Vector3(0, 0, 0);
  109. }
  110. move +=
  111. new Vector3(playerInput.MoveInput.x, 0, playerInput.MoveInput.y)
  112. * Time.deltaTime
  113. * speed;
  114. // �ƶ�����
  115. Move();
  116. CameraLimitation();
  117. UpdateControlRotation();
  118. }
  119. void Jump()
  120. {
  121. // ������Ծ�ٶ��Դﵽָ������Ծ�߶�
  122. verticalVelocity = Mathf.Sqrt(-2.0f * gravity * jumpHeight);
  123. }
  124. void ApplyGravity()
  125. {
  126. // Ӧ��������ģ������Ĵ�ֱ�˶�
  127. verticalVelocity += gravity * Time.deltaTime;
  128. // ���ݴ�ֱ�ٶ��ƶ�����
  129. if (verticalVelocity < 0.1f && verticalVelocity > -0.1f && state == PlayerState.JumpUp)
  130. {
  131. Debug.Log("*************");
  132. state = PlayerState.JumpDown;
  133. }
  134. //Debug.Log(verticalVelocity * 100);
  135. gravityMove = new Vector3(0, verticalVelocity * Time.deltaTime, 0);
  136. }
  137. void Move()
  138. {
  139. if (playerInput.MoveInput != Vector2.zero)
  140. {
  141. Vector3 dir = Vector3.zero;
  142. //Debug.Log(playerInput.MoveInput);
  143. if (playerInput.MoveInput.x > 0)
  144. {
  145. dir = Vector3.ProjectOnPlane(camRoot.right, Vector3.up);
  146. }
  147. if (playerInput.MoveInput.x < 0)
  148. {
  149. dir += Vector3.ProjectOnPlane(-camRoot.right, Vector3.up);
  150. }
  151. if (playerInput.MoveInput.y > 0)
  152. {
  153. dir += Vector3.ProjectOnPlane(camRoot.forward, Vector3.up);
  154. }
  155. if (playerInput.MoveInput.y < 0)
  156. {
  157. dir += Vector3.ProjectOnPlane(-camRoot.forward, Vector3.up);
  158. }
  159. //Debug.Log(dir);
  160. Vector3 targetDir = Vector3.RotateTowards(
  161. transform.forward,
  162. //camera.forward.ProjectOntoPlane(Vector3.up),
  163. dir,
  164. 10,
  165. 0f
  166. );
  167. transform.rotation = Quaternion.Slerp(
  168. transform.rotation,
  169. Quaternion.LookRotation(targetDir),
  170. Time.deltaTime * 15f
  171. );
  172. //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
  173. //Quaternion r = Quaternion.Euler(0, a, 0);
  174. //transform.eulerAngles = r * transform.eulerAngles;
  175. }
  176. //var m = move.magnitude * camera.forward + gravityMove;
  177. //Debug.Log(move + "*******" + "************" + m);
  178. var angle = AngleSigned(
  179. Vector3.forward,
  180. Vector3.ProjectOnPlane(camRoot.forward, Vector3.up),
  181. transform.up
  182. );
  183. Quaternion rot = Quaternion.Euler(0, angle, 0);
  184. //Vector3 move2 = camera.transform.TransformDirection(move);
  185. //Debug.Log(move.magnitude + "***********" + gravityMove.magnitude);
  186. //var m = move * 0.1f + lastMove * 0.9f;
  187. var rm = rot * move;
  188. if (gravityMove != Vector3.zero)
  189. {
  190. rm *= 2.5f;
  191. }
  192. rm = rm * 0.1f + lastMove * 0.9f;
  193. //if (state == PlayerState.JumpDown)
  194. //{
  195. // character.Move(-colliderDir * Time.deltaTime);
  196. //}
  197. character.Move(rm + gravityMove);
  198. lastMove = rm;
  199. controller.speed = rm.magnitude * 300;
  200. camRoot.position = character.transform.position + new Vector3(0, 1.148f, 0);
  201. }
  202. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
  203. {
  204. return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))
  205. * Mathf.Rad2Deg;
  206. }
  207. private Vector3 pointBottom;
  208. private Vector3 pointTop;
  209. private float radius = 0.5f;
  210. private Collider[] colliders;
  211. bool IsGrounded()
  212. {
  213. // �������������Ӽ�������Ƿ��ڵ����ϵ��߼�
  214. // �������߼�⡢��ײ������
  215. var pos = transform.position;
  216. radius = character.radius * 0.9f;
  217. pointBottom = pos + transform.up * radius - transform.up * 0.1f;
  218. pointTop = pos + transform.up * character.height - transform.up * radius;
  219. colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, groundLayer);
  220. Debug.DrawLine(pointBottom, pointTop, Color.red);
  221. if (colliders.Length != 0)
  222. {
  223. return true;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. }
  230. private void UpdateControlRotation()
  231. {
  232. if (
  233. playerInput.touch0Input != Vector2.zero
  234. && playerInput.MoveInput == Vector2.zero
  235. && !EventSystem.current.IsPointerOverGameObject()
  236. )
  237. {
  238. //camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
  239. angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
  240. angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
  241. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  242. //ʹ�ò�ֵƽ����ת
  243. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  244. }
  245. if (
  246. playerInput.touch1Input != Vector2.zero
  247. && !EventSystem.current.IsPointerOverGameObject()
  248. )
  249. {
  250. angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
  251. angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
  252. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  253. //ʹ�ò�ֵƽ����ת
  254. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  255. }
  256. angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
  257. angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
  258. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  259. //ʹ�ò�ֵƽ����ת
  260. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 10);
  261. }
  262. private static float angleClamp(float angle, float min, float max)
  263. { //һ�����ƽǶ������Сֵ�ķ���
  264. if (angle < -360)
  265. angle += 360;
  266. if (angle > 360)
  267. angle -= 360;
  268. return Mathf.Clamp(angle, min, max);
  269. }
  270. /// <summary>
  271. /// �����˳ƾ�ͷ�����ƶ�
  272. /// </summary>
  273. private void CameraLimitation()
  274. {
  275. float cosAngle = Vector3.Dot(m_camera.forward, Vector3.up);
  276. float angleRad = Mathf.Asin(cosAngle);
  277. float angleDeg = Mathf.Rad2Deg * angleRad;
  278. float sign = Mathf.Sign(Vector3.Dot(Vector3.Cross(m_camera.forward, Vector3.up), Vector3.up));
  279. float finalAngle = sign * angleDeg;
  280. //float moveDistance = Mathf.Sin(Mathf.Deg2Rad * Mathf.Clamp(finalAngle, 0f, 10)) * 2;
  281. float z, obsDistance=0,camDistance=0;
  282. //Debug.Log(finalAngle);
  283. //���߼�����
  284. //Ray ray = new Ray(head.position, m_camera.position - head.position);
  285. //RaycastHit temphit;
  286. //if (Physics.Raycast(ray, out temphit, 10f))
  287. //{
  288. // if (temphit.collider.name != "Main Camera")
  289. // {
  290. // isBlock = true;
  291. // hit = temphit;
  292. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
  293. // }
  294. // else
  295. // {
  296. // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  297. // }
  298. // Debug.Log(temphit.collider.name);
  299. //}
  300. //if (isBlock)
  301. //{
  302. // ////������ת��ͷ����
  303. // //if (finalAngle > 1)
  304. // //{
  305. // // z = finalAngle * 0.1f;
  306. // // camDistance = Vector3.Distance(camInitPos + new Vector3(0, 0, z),head.position);
  307. // // Debug.DrawLine(temphit.point, head.position, Color.red);
  308. // // Debug.DrawLine(camInitPos + new Vector3(0, 0, z), head.position, Color.blue);
  309. // // Debug.Log(hit.collider.name+"))"+obsDistance + " // " + camDistance);
  310. // // //if (obsDistance < camDistance && obsDistance > 0)
  311. // // //{
  312. // // // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
  313. // // //}
  314. // // //else
  315. // // //{
  316. // // // m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  317. // // //}
  318. // //}
  319. // //else
  320. // {
  321. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  322. // }
  323. //}
  324. //else
  325. //{
  326. //������ת��ͷ����
  327. if (finalAngle > 1)
  328. {
  329. z = finalAngle * 0.1f;
  330. m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  331. }
  332. else
  333. {
  334. m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  335. }
  336. //}
  337. }
  338. }