MoveController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  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 float oriDistance;
  18. private bool isGrounded = false;
  19. private bool hasLand = 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. public enum PlayerState
  28. {
  29. Ground,
  30. JumpUp,
  31. JumpDown,
  32. }
  33. private CharacterController character;
  34. public PlayerState state;
  35. // Start is called before the first frame update
  36. void Start()
  37. {
  38. controller = GetComponent<AnimatorController>();
  39. character = GetComponent<CharacterController>();
  40. playerInput=GetComponent<PlayerInputComponent>();
  41. angleX = Vector3.Angle(Vector3.right, camRoot.right);
  42. angleY = Vector3.Angle(Vector3.up, camRoot.up);
  43. character.enabled = true;
  44. head = transform.Find("Head");
  45. m_camera = camRoot.transform.Find("Main Camera");
  46. camInitPos = m_camera.localPosition;
  47. oriDistance = Vector3.Distance(head.position, m_camera.position-m_camera.forward*2);
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. if (StepManager.isFaceBuidling)
  53. playerInput.InputReset();
  54. CharacterMovement();
  55. }
  56. /// <summary>
  57. /// ��ɫ�ƶ���ת���߼�
  58. /// </summary>
  59. void CharacterMovement()
  60. {
  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 && 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. //Debug.Log(playerInput.MoveInput);
  142. if (playerInput.MoveInput.x > 0)
  143. {
  144. dir = Vector3.ProjectOnPlane(camRoot.right, Vector3.up);
  145. }
  146. if (playerInput.MoveInput.x < 0)
  147. {
  148. dir += Vector3.ProjectOnPlane(-camRoot.right, Vector3.up);
  149. }
  150. if (playerInput.MoveInput.y > 0)
  151. {
  152. dir += Vector3.ProjectOnPlane(camRoot.forward, Vector3.up);
  153. }
  154. if (playerInput.MoveInput.y < 0)
  155. {
  156. dir += Vector3.ProjectOnPlane(-camRoot.forward, Vector3.up);
  157. }
  158. //Debug.Log(dir);
  159. Vector3 targetDir = Vector3.RotateTowards(
  160. transform.forward,
  161. //camera.forward.ProjectOntoPlane(Vector3.up),
  162. dir,
  163. 10,
  164. 0f
  165. );
  166. transform.rotation = Quaternion.Slerp(
  167. transform.rotation,
  168. Quaternion.LookRotation(targetDir),
  169. Time.deltaTime * 15f
  170. );
  171. //var a = Vector3.Angle(transform.forward.normalized, playerInput.MoveInput.normalized);
  172. //Quaternion r = Quaternion.Euler(0, a, 0);
  173. //transform.eulerAngles = r * transform.eulerAngles;
  174. }
  175. //var m = move.magnitude * camera.forward + gravityMove;
  176. //Debug.Log(move + "*******" + "************" + m);
  177. var angle = AngleSigned(
  178. Vector3.forward,
  179. Vector3.ProjectOnPlane(camRoot.forward, Vector3.up),
  180. transform.up
  181. );
  182. Quaternion rot = Quaternion.Euler(0, angle, 0);
  183. //Vector3 move2 = camera.transform.TransformDirection(move);
  184. //Debug.Log(move.magnitude + "***********" + gravityMove.magnitude);
  185. //var m = move * 0.1f + lastMove * 0.9f;
  186. var rm = rot * move;
  187. if (gravityMove != Vector3.zero)
  188. {
  189. rm *= 2.5f;
  190. }
  191. rm = rm * 0.1f + lastMove * 0.9f;
  192. //if (state == PlayerState.JumpDown)
  193. //{
  194. // character.Move(-colliderDir * Time.deltaTime);
  195. //}
  196. character.Move(rm + gravityMove);
  197. lastMove = rm;
  198. controller.speed = rm.magnitude * 300;
  199. camRoot.position = character.transform.position + new Vector3(0, 1.148f, 0);
  200. }
  201. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
  202. {
  203. return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2))
  204. * Mathf.Rad2Deg;
  205. }
  206. private Vector3 pointBottom;
  207. private Vector3 pointTop;
  208. private float radius = 0.5f;
  209. private Collider[] colliders;
  210. bool IsGrounded()
  211. {
  212. // �������������Ӽ�������Ƿ��ڵ����ϵ��߼�
  213. // �������߼�⡢��ײ������
  214. var pos = transform.position;
  215. radius = character.radius * 0.9f;
  216. pointBottom = pos + transform.up * radius - transform.up * 0.1f;
  217. pointTop = pos + transform.up * character.height - transform.up * radius;
  218. colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, groundLayer);
  219. Debug.DrawLine(pointBottom, pointTop, Color.red);
  220. if (colliders.Length != 0)
  221. {
  222. return true;
  223. }
  224. else
  225. {
  226. return false;
  227. }
  228. }
  229. private void UpdateControlRotation()
  230. {
  231. if (
  232. playerInput.touch0Input != Vector2.zero
  233. && playerInput.MoveInput == Vector2.zero
  234. && !EventSystem.current.IsPointerOverGameObject()
  235. )
  236. {
  237. //camera.eulerAngles += (new Vector3(playerInput.touch0Input.y, playerInput.touch0Input.x, 0)) * 0.1f;
  238. angleX += playerInput.touch0Input.x * 10 * Time.fixedDeltaTime;
  239. angleY -= playerInput.touch0Input.y * 10 * Time.fixedDeltaTime;
  240. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  241. //ʹ�ò�ֵƽ����ת
  242. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  243. }
  244. if (
  245. playerInput.touch1Input != Vector2.zero
  246. && !EventSystem.current.IsPointerOverGameObject()
  247. )
  248. {
  249. angleX += playerInput.touch1Input.x * 10 * Time.fixedDeltaTime;
  250. angleY -= playerInput.touch1Input.y * 10 * Time.fixedDeltaTime;
  251. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  252. //ʹ�ò�ֵƽ����ת
  253. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 100);
  254. }
  255. angleX += playerInput.CameraInput.x * 10 * Time.fixedDeltaTime;
  256. angleY -= playerInput.CameraInput.y * 10 * Time.fixedDeltaTime;
  257. angleY = angleClamp(angleY, -45, 60);//�����ת�Ƕ�����
  258. //ʹ�ò�ֵƽ����ת
  259. camRoot.rotation = Quaternion.Lerp(camRoot.rotation, Quaternion.Euler(angleY, angleX, 0), Time.fixedDeltaTime * 10);
  260. }
  261. private static float angleClamp(float angle, float min, float max)
  262. { //һ�����ƽǶ������Сֵ�ķ���
  263. if (angle < -360)
  264. angle += 360;
  265. if (angle > 360)
  266. angle -= 360;
  267. return Mathf.Clamp(angle, min, max);
  268. }
  269. /// <summary>
  270. /// �����˳ƾ�ͷ�����ƶ�
  271. /// </summary>
  272. private void CameraLimitation()
  273. {
  274. float cosAngle = Vector3.Dot(m_camera.forward, Vector3.up);
  275. float angleRad = Mathf.Asin(cosAngle);
  276. float angleDeg = Mathf.Rad2Deg * angleRad;
  277. float sign = Mathf.Sign(Vector3.Dot(Vector3.Cross(m_camera.forward, Vector3.up), Vector3.up));
  278. float finalAngle = sign * angleDeg;
  279. //float moveDistance = Mathf.Sin(Mathf.Deg2Rad * Mathf.Clamp(finalAngle, 0f, 10)) * 2;
  280. float z, obsDistance = 0, camDistance = 0;
  281. //Debug.Log(finalAngle);
  282. //射线检测是否有障碍物
  283. Ray ray = new Ray(head.position, m_camera.position - head.position);
  284. RaycastHit[] temphit;
  285. RaycastHit hit;
  286. var cos = Physics.Raycast(ray, out hit, oriDistance, ~(1 << 6|1<<18));
  287. if (cos)
  288. {
  289. Debug.Log(hit.collider.name);
  290. m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  291. }
  292. else
  293. {
  294. m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  295. }
  296. //RaycastHit[] temphit;
  297. //var cos = Physics.RaycastAll(head.position, m_camera.position - head.position, oriDistance, ~(1 << 6));
  298. //if (cos.Length>0)
  299. //{
  300. // //Debug.Log(temphit.collider.name);
  301. // //m_camera.position = Vector3.Lerp(m_camera.position, temphit.point, 0.5f);
  302. // RaycastHit hit = cos[0];
  303. // float dis = 0;
  304. // for (int i = 1; i<cos.Length; i++)
  305. // {
  306. // if(dis< Vector3.Distance(cos[i].point, head.position))
  307. // {
  308. // dis = Vector3.Distance(cos[i].point, head.position);
  309. // hit = cos[i];
  310. // }
  311. // }
  312. // if(!hit.collider.name.Contains("Main"))
  313. // {
  314. // Debug.Log(hit.collider.name);
  315. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  316. // }
  317. // //if (temphit.collider.name != "Main Camera")
  318. // //{
  319. // //}
  320. // //else
  321. // //{
  322. // // //如果射线没有打到任何collider,或者打到的不是墙体,则将相机和角色之间的距离还原
  323. // // Vector3 p = (m_camera.position - head.position).normalized * oriDistance;
  324. // // Vector3 newPoint = head.position + p;
  325. // // m_camera.position = Vector3.Lerp(m_camera.position, newPoint, 0.5f);
  326. // //}
  327. //}
  328. //else
  329. //{
  330. // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  331. //}
  332. //else
  333. //{
  334. // //如果射线没有打到任何collider,或者打到的不是墙体,则将相机和角色之间的距离还原
  335. // Vector3 p = (m_camera.position - head.position).normalized * oriDistance;
  336. // Vector3 newPoint = head.position + p;
  337. // m_camera.localPosition = Vector3.Lerp(m_camera.position, new Vector3(0,1.48f,-5), 0.5f);
  338. //}
  339. //
  340. //}
  341. //if (isBlock)
  342. //{
  343. ////������ת��ͷ����
  344. //if (finalAngle > 1)
  345. //{
  346. // z = finalAngle * 0.1f;
  347. // camDistance = Vector3.Distance(camInitPos + new Vector3(0, 0, z),head.position);
  348. // Debug.DrawLine(temphit.point, head.position, Color.red);
  349. // Debug.DrawLine(camInitPos + new Vector3(0, 0, z), head.position, Color.blue);
  350. // Debug.Log(hit.collider.name+"))"+obsDistance + " // " + camDistance);
  351. // //if (obsDistance < camDistance && obsDistance > 0)
  352. // //{
  353. // // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.1f);
  354. // //}
  355. // //else
  356. // //{
  357. // // m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  358. // //}
  359. //}
  360. //else
  361. // {
  362. // m_camera.position = Vector3.Lerp(m_camera.position, hit.point, 0.5f);
  363. // }
  364. //}
  365. //else
  366. //{
  367. //旋转
  368. if (finalAngle > 1)
  369. {
  370. z = finalAngle * 0.1f;
  371. var pos = camInitPos + new Vector3(0, 0, z);
  372. var dis1 =Vector3.Distance(head.position, pos);
  373. var dis2 =Vector3.Distance(m_camera.position, head.position);
  374. if(!cos)
  375. m_camera.localPosition = camInitPos + new Vector3(0, 0, z);
  376. }
  377. if (m_camera.localPosition.z > -0.4f)
  378. {
  379. m_camera.localPosition = new Vector3(m_camera.localPosition.x, m_camera.localPosition.y,-0.4f);
  380. }
  381. //else
  382. //{
  383. // m_camera.localPosition = Vector3.Lerp(m_camera.localPosition, camInitPos, 0.5f);
  384. //}
  385. //}
  386. }
  387. }