MoveController.cs 15 KB

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