MoveController.cs 14 KB

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