MoveController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.InputSystem.HID;
  5. using static UnityEditor.ShaderGraph.Internal.KeywordDependentCollection;
  6. public class MoveController : MonoBehaviour
  7. {
  8. public PlayerInputComponent playerInput;
  9. public Transform camRoot;
  10. public float jumpHeight = 2.0f;
  11. public float gravity = -13.524f;
  12. private int groundLayer = ~(1 << 6);
  13. private float speed = 5f;
  14. private float verticalVelocity = 0.0f;
  15. private float angleX;
  16. private float angleY;
  17. private float jumpInterval = 0.5f;
  18. private float jumpTime = 0.5f;
  19. private float oriDistance;
  20. private bool isGrounded = false;
  21. private bool hasLand = false;
  22. private AnimatorController controller;
  23. private Transform head;
  24. private Transform m_camera;
  25. private Vector3 move;
  26. private Vector3 gravityMove;
  27. private Vector3 lastMove;
  28. private Vector3 camInitPos;
  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. oriDistance = Vector3.Distance(head.position, m_camera.position-m_camera.forward*2);
  50. }
  51. // Update is called once per frame
  52. void Update()
  53. {
  54. if (StepManager.isFaceBuidling)
  55. playerInput.InputReset();
  56. CharacterMovement();
  57. }
  58. /// <summary>
  59. /// ��ɫ�ƶ���ת���߼�
  60. /// </summary>
  61. void CharacterMovement()
  62. {
  63. if (hasLand)
  64. {
  65. jumpTime += Time.deltaTime;
  66. }
  67. // ����Ƿ��ڵ�����
  68. //isGrounded = IsGrounded();
  69. isGrounded =character.isGrounded|| IsGrounded();
  70. if (isGrounded && state == PlayerState.JumpDown)
  71. {
  72. verticalVelocity = 0;
  73. state = PlayerState.Ground;
  74. hasLand = true;
  75. }
  76. //Debug.Log(isGrounded);
  77. if (state == PlayerState.Ground)
  78. {
  79. // ���¿ո��������Ծ
  80. if (playerInput.JumpInput)
  81. {
  82. //state = PlayerState.JumpUp;
  83. //Jump();
  84. //controller.jump = true;
  85. //jumpTime = 0;
  86. //hasLand = false;
  87. if (jumpTime >= jumpInterval)
  88. {
  89. state = PlayerState.JumpUp;
  90. Jump();
  91. controller.jump = true;
  92. jumpTime = 0;
  93. hasLand = false;
  94. }
  95. }
  96. }
  97. if (!isGrounded && state != PlayerState.JumpUp)
  98. {
  99. state = PlayerState.JumpDown;
  100. }
  101. move = Vector3.zero;
  102. if (state != PlayerState.Ground)
  103. {
  104. // Ӧ������
  105. ApplyGravity();
  106. }
  107. else
  108. {
  109. gravityMove = new Vector3(0, 0, 0);
  110. }
  111. move +=
  112. new Vector3(playerInput.MoveInput.x, 0, playerInput.MoveInput.y)
  113. * Time.deltaTime
  114. * speed;
  115. // �ƶ�����
  116. Move();
  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. CameraLimitation();
  140. if (playerInput.MoveInput != Vector2.zero)
  141. {
  142. Vector3 dir = Vector3.zero;
  143. //Debug.Log(playerInput.MoveInput);
  144. if (playerInput.MoveInput.x > 0)
  145. {
  146. dir = Vector3.ProjectOnPlane(camRoot.right, Vector3.up);
  147. }
  148. if (playerInput.MoveInput.x < 0)
  149. {
  150. dir += Vector3.ProjectOnPlane(-camRoot.right, Vector3.up);
  151. }
  152. if (playerInput.MoveInput.y > 0)
  153. {
  154. dir += Vector3.ProjectOnPlane(camRoot.forward, Vector3.up);
  155. }
  156. if (playerInput.MoveInput.y < 0)
  157. {
  158. dir += Vector3.ProjectOnPlane(-camRoot.forward, Vector3.up);
  159. }
  160. //Debug.Log(dir);
  161. Vector3 targetDir = Vector3.RotateTowards(
  162. transform.forward,
  163. //camera.forward.ProjectOntoPlane(Vector3.up),
  164. dir,
  165. 10,
  166. 0f
  167. );
  168. transform.rotation = Quaternion.Slerp(
  169. transform.rotation,
  170. Quaternion.LookRotation(targetDir),
  171. Time.deltaTime * 15f
  172. );
  173. //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
  174. //Quaternion r = Quaternion.Euler(0, a, 0);
  175. //transform.eulerAngles = r * transform.eulerAngles;
  176. }
  177. //var m = move.magnitude * camera.forward + gravityMove;
  178. //Debug.Log(move + "*******" + "************" + m);
  179. var angle = AngleSigned(
  180. Vector3.forward,
  181. Vector3.ProjectOnPlane(camRoot.forward, Vector3.up),
  182. transform.up
  183. );
  184. Quaternion rot = Quaternion.Euler(0, angle, 0);
  185. //Vector3 move2 = camera.transform.TransformDirection(move);
  186. //Debug.Log(move.magnitude + "***********" + gravityMove.magnitude);
  187. //var m = move * 0.1f + lastMove * 0.9f;
  188. var rm = rot * move;
  189. if (gravityMove != Vector3.zero)
  190. {
  191. rm *= 2.5f;
  192. }
  193. rm = rm * 0.1f + lastMove * 0.9f;
  194. //if (state == PlayerState.JumpDown)
  195. //{
  196. // character.Move(-colliderDir * Time.deltaTime);
  197. //}
  198. character.Move(rm + gravityMove);
  199. lastMove = rm;
  200. controller.speed = rm.magnitude * 300;
  201. camRoot.position = character.transform.position + new Vector3(0, 1.148f, 0);
  202. }
  203. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
  204. {
  205. return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))
  206. * Mathf.Rad2Deg;
  207. }
  208. private Vector3 pointBottom;
  209. private Vector3 pointTop;
  210. private float radius = 0.5f;
  211. private Collider[] colliders;
  212. bool IsGrounded()
  213. {
  214. // �������������Ӽ�������Ƿ��ڵ����ϵ��߼�
  215. // �������߼�⡢��ײ������
  216. var pos = transform.position;
  217. radius = character.radius * 0.9f;
  218. pointBottom = pos + transform.up * radius - transform.up * 0.1f;
  219. pointTop = pos + transform.up * character.height - transform.up * radius;
  220. colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, groundLayer);
  221. Debug.DrawLine(pointBottom, pointTop, Color.red);
  222. if (colliders.Length != 0)
  223. {
  224. return true;
  225. }
  226. else
  227. {
  228. return false;
  229. }
  230. }
  231. private void UpdateControlRotation()
  232. {
  233. if (
  234. playerInput.touch0Input != Vector2.zero
  235. && playerInput.MoveInput == Vector2.zero
  236. && !EventSystem.current.IsPointerOverGameObject()
  237. )
  238. {
  239. //camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
  240. angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
  241. angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
  242. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  243. //ʹ�ò�ֵƽ����ת
  244. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  245. }
  246. if (
  247. playerInput.touch1Input != Vector2.zero
  248. && !EventSystem.current.IsPointerOverGameObject()
  249. )
  250. {
  251. angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
  252. angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
  253. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  254. //ʹ�ò�ֵƽ����ת
  255. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  256. }
  257. angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
  258. angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
  259. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  260. //ʹ�ò�ֵƽ����ת
  261. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 10);
  262. }
  263. private static float angleClamp(float angle, float min, float max)
  264. { //һ�����ƽǶ������Сֵ�ķ���
  265. if (angle < -360)
  266. angle += 360;
  267. if (angle > 360)
  268. angle -= 360;
  269. return Mathf.Clamp(angle, min, max);
  270. }
  271. /// <summary>
  272. /// �����˳ƾ�ͷ�����ƶ�
  273. /// </summary>
  274. private void CameraLimitation()
  275. {
  276. float cosAngle = Vector3.Dot(m_camera.forward, Vector3.up);
  277. float angleRad = Mathf.Asin(cosAngle);
  278. float angleDeg = Mathf.Rad2Deg * angleRad;
  279. float sign = Mathf.Sign(Vector3.Dot(Vector3.Cross(m_camera.forward, Vector3.up), Vector3.up));
  280. float finalAngle = sign * angleDeg;
  281. //float moveDistance = Mathf.Sin(Mathf.Deg2Rad * Mathf.Clamp(finalAngle, 0f, 10)) * 2;
  282. float z, obsDistance = 0, camDistance = 0;
  283. //Debug.Log(finalAngle);
  284. //射线检测是否有障碍物
  285. Ray ray = new Ray(head.position, m_camera.position - head.position);
  286. RaycastHit[] temphit;
  287. RaycastHit hit;
  288. var cos = Physics.Raycast(ray, out hit, oriDistance, ~(1 << 6|1<<18));
  289. if (cos)
  290. {
  291. Debug.Log(hit.collider.name);
  292. m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  293. }
  294. else
  295. {
  296. m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  297. }
  298. //RaycastHit[] temphit;
  299. //var cos = Physics.RaycastAll(head.position, m_camera.position - head.position, oriDistance, ~(1 << 6));
  300. //if (cos.Length>0)
  301. //{
  302. // //Debug.Log(temphit.collider.name);
  303. // //m_camera.position = Vector3.Lerp(m_camera.position, temphit.point, 0.5f);
  304. // RaycastHit hit = cos[0];
  305. // float dis = 0;
  306. // for (int i = 1; i<cos.Length; i++)
  307. // {
  308. // if(dis< Vector3.Distance(cos[i].point, head.position))
  309. // {
  310. // dis = Vector3.Distance(cos[i].point, head.position);
  311. // hit = cos[i];
  312. // }
  313. // }
  314. // if(!hit.collider.name.Contains("Main"))
  315. // {
  316. // Debug.Log(hit.collider.name);
  317. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  318. // }
  319. // //if (temphit.collider.name != "Main Camera")
  320. // //{
  321. // //}
  322. // //else
  323. // //{
  324. // // //如果射线没有打到任何collider,或者打到的不是墙体,则将相机和角色之间的距离还原
  325. // // Vector3 p = (m_camera.position - head.position).normalized * oriDistance;
  326. // // Vector3 newPoint = head.position + p;
  327. // // m_camera.position = Vector3.Lerp(m_camera.position, newPoint, 0.5f);
  328. // //}
  329. //}
  330. //else
  331. //{
  332. // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  333. //}
  334. //else
  335. //{
  336. // //如果射线没有打到任何collider,或者打到的不是墙体,则将相机和角色之间的距离还原
  337. // Vector3 p = (m_camera.position - head.position).normalized * oriDistance;
  338. // Vector3 newPoint = head.position + p;
  339. // m_camera.localPosition = Vector3.Lerp(m_camera.position, new Vector3(0,1.48f,-5), 0.5f);
  340. //}
  341. //
  342. //}
  343. //if (isBlock)
  344. //{
  345. ////������ת��ͷ����
  346. //if (finalAngle > 1)
  347. //{
  348. // z = finalAngle * 0.1f;
  349. // camDistance = Vector3.Distance(camInitPos + new Vector3(0, 0, z),head.position);
  350. // Debug.DrawLine(temphit.point, head.position, Color.red);
  351. // Debug.DrawLine(camInitPos + new Vector3(0, 0, z), head.position, Color.blue);
  352. // Debug.Log(hit.collider.name+"))"+obsDistance + " // " + camDistance);
  353. // //if (obsDistance < camDistance && obsDistance > 0)
  354. // //{
  355. // // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
  356. // //}
  357. // //else
  358. // //{
  359. // // m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  360. // //}
  361. //}
  362. //else
  363. // {
  364. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  365. // }
  366. //}
  367. //else
  368. //{
  369. //旋转
  370. if (finalAngle > 1)
  371. {
  372. z = finalAngle * 0.1f;
  373. var pos = camInitPos + new Vector3(0, 0, z);
  374. var dis1 =Vector3.Distance(head.position, pos);
  375. var dis2 =Vector3.Distance(m_camera.position, head.position);
  376. if(!cos)
  377. m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  378. }
  379. if (m_camera.localPosition.z > -0.4f)
  380. {
  381. m_camera.localPosition = new Vector3(m_camera.localPosition.x, m_camera.localPosition.y,-0.4f);
  382. }
  383. //else
  384. //{
  385. // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  386. //}
  387. //}
  388. }
  389. }