AimCrossHair.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.UI;
  6. public class AimCrossHair : MonoBehaviour
  7. {
  8. public Camera mainCamera;
  9. [HideInInspector]
  10. public PlayerType playerType = PlayerType.FirstPlayer;
  11. [NonSerialized] public bool outboundLeft;
  12. [NonSerialized] public bool outboundRight;
  13. [NonSerialized] public bool outboundUp;
  14. [NonSerialized] public bool outboundDown;
  15. private bool visiable = false;
  16. void Start()
  17. {
  18. int currentShootLevel = UserSettings.ins.shootLevel;
  19. shootBackTime = new int[] { 0, 2, 5 }[currentShootLevel];
  20. shootOffsetAngleScale = new float[] { 0, 10f, 10f }[currentShootLevel];
  21. }
  22. void OnDestroy()
  23. {
  24. }
  25. void Update()
  26. {
  27. SetVisiable(onlyShow && GameController.ins.GetArmBowDoublePlayer(playerType).IsCanShoot());
  28. //if (GetPlayerIndex() == 0)
  29. //{
  30. // UpdatePositionByModuleRotation(mouseTest.Update(0),new Vector4(0,Screen.width*0.5f,Screen.height,0));
  31. //}
  32. //else if (GetPlayerIndex() == 1)
  33. //{
  34. // UpdatePositionByModuleRotation(mouseTest.Update(1), new Vector4(Screen.width*0.5f, Screen.width, Screen.height, 0));
  35. //}
  36. //if (GameController.ins.gameStart && !GameController.ins.gameOver)
  37. //{
  38. // transform.localScale = Vector3.one;
  39. //}
  40. //else transform.localScale = Vector3.zero;
  41. }
  42. private bool onlyShow = true;
  43. public void SetOnlyShow(bool onlyShow)
  44. {
  45. this.onlyShow = onlyShow;
  46. }
  47. public bool GetOnlyShow()
  48. {
  49. return this.onlyShow;
  50. }
  51. int GetPlayerIndex()
  52. {
  53. return playerType == PlayerType.FirstPlayer ? 0 : 1; //Array.IndexOf(GameController.ins.aimCrossHairs, this);
  54. }
  55. //MouseTest mouseTest = new MouseTest();
  56. class MouseTest
  57. {
  58. Quaternion quat = Quaternion.identity;
  59. float moveSensitivity = 30;
  60. public Quaternion Update(int type)
  61. {
  62. if (type == 0)
  63. {
  64. if (Input.GetKey(KeyCode.A))
  65. {
  66. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  67. }
  68. else if (Input.GetKey(KeyCode.D))
  69. {
  70. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  71. }
  72. else if (Input.GetKey(KeyCode.W))
  73. {
  74. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  75. }
  76. else if (Input.GetKey(KeyCode.S))
  77. {
  78. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  79. }
  80. }
  81. else if (type == 1)
  82. {
  83. if (Input.GetKey(KeyCode.LeftArrow))
  84. {
  85. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  86. }
  87. else if (Input.GetKey(KeyCode.RightArrow))
  88. {
  89. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  90. }
  91. else if (Input.GetKey(KeyCode.UpArrow))
  92. {
  93. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  94. }
  95. else if (Input.GetKey(KeyCode.DownArrow))
  96. {
  97. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  98. }
  99. }
  100. return quat;
  101. }
  102. }
  103. Quaternion currentRotation = Quaternion.identity;
  104. public void UpdatePositionByModuleRotation(Quaternion moduleRotation,Vector4 bound)
  105. {
  106. // 限制模块欧拉角范围
  107. Vector3 moduleAngles = moduleRotation.eulerAngles;
  108. moduleAngles.x = Mathf.Clamp(moduleAngles.x > 180 ? moduleAngles.x - 360 : moduleAngles.x, -80, 80);
  109. moduleAngles.y = Mathf.Clamp(moduleAngles.y > 180 ? moduleAngles.y - 360 : moduleAngles.y, -80, 80);
  110. moduleRotation = Quaternion.Euler(moduleAngles);
  111. currentRotation = moduleRotation;
  112. // 计算准心的世界旋转
  113. Quaternion rotation = mainCamera.transform.rotation * moduleRotation;
  114. // 计算准心的世界方向
  115. Vector3 direction = rotation * Vector3.forward;
  116. // 计算准心的世界坐标
  117. Vector3 position = mainCamera.transform.position + direction;
  118. // 转为屏幕坐标
  119. Vector3 screenPos = mainCamera.WorldToScreenPoint(position);
  120. // 标记是否超出屏幕边界
  121. outboundLeft = screenPos.x < bound.x;//0
  122. outboundRight = screenPos.x > bound.y;//Screen.width
  123. outboundUp = screenPos.y > bound.z;//Screen.height
  124. outboundDown = screenPos.y < bound.w; //0
  125. // 根据需要的范围进行限制
  126. screenPos.x = Mathf.Clamp(screenPos.x, bound.x, bound.y); //x->y
  127. screenPos.y = Mathf.Clamp(screenPos.y, bound.w, bound.z); //w->z
  128. // 设置准心的位置
  129. //transform.position = screenPos;
  130. }
  131. //射箭姿态记录索引倒退数,根据射击难度制造误差
  132. [NonSerialized] public int shootBackTime = 0;
  133. [NonSerialized] public float shootOffsetAngleScale = 0;
  134. //过去几帧的镜头值记录
  135. // Quaternion[] cameraRotations = new Quaternion[6];
  136. // int cameraRotationHasRecordCount = 0;
  137. public RaycastHit GetRaycastHit()
  138. {
  139. float maxDistance = 100;
  140. int layerMask = 1 << 8; //TargetLayerMask
  141. if (GetPlayerIndex() == 0)
  142. {
  143. layerMask = 1 << 6; // Target1
  144. }
  145. else if (GetPlayerIndex() == 1)
  146. {
  147. layerMask = 1 << 7; // Target2
  148. }
  149. RaycastHit raycastHit;
  150. Ray ray = mainCamera.ScreenPointToRay(crosshairTran.position, Camera.MonoOrStereoscopicEye.Mono);
  151. Debug.DrawRay(ray.origin, ray.direction * 1000, Color.yellow,5);
  152. Physics.Raycast(ray.origin, ray.direction, out raycastHit, maxDistance, layerMask);
  153. //UnityEditor.EditorApplication.isPaused = true;
  154. return raycastHit;
  155. }
  156. private void ChangeLayer(Transform transform, string layer)
  157. {
  158. if (transform.childCount > 0)//如果子物体存在
  159. {
  160. for (int i = 0; i < transform.childCount; i++)//遍历子物体是否还有子物体
  161. {
  162. ChangeLayer(transform.GetChild(i), layer);//这里是只将最后一个无子物体的对象设置层级
  163. }
  164. transform.gameObject.layer = LayerMask.NameToLayer(layer);//将存在的子物体遍历结束后需要把当前子物体节点进行层级设置
  165. }
  166. else //无子物体
  167. {
  168. transform.gameObject.layer = LayerMask.NameToLayer(layer);
  169. }
  170. if (transform.name == "_hunse_jian") {
  171. Debug.Log(transform.gameObject.layer);
  172. }
  173. }
  174. #region 更新中心准心
  175. RectTransform _parentRTF;
  176. RectTransform parentRTF
  177. {
  178. get
  179. {
  180. if (!_parentRTF)
  181. {
  182. _parentRTF = transform.parent.GetComponent<RectTransform>();
  183. }
  184. return _parentRTF;
  185. }
  186. }
  187. Vector3 centerPoint = new Vector3(0, 1, 0);
  188. Vector3 targetPosition;
  189. public Transform crosshairTran;
  190. public void UpdatePostionWhenFixedCamera(BowCameraDoublePlayer bowCameraDoublePlayer)
  191. {
  192. //if (!visiable) return;
  193. Transform transform = bowCameraDoublePlayer.transform;
  194. Ray ray = new Ray(transform.position, transform.forward);
  195. RaycastHit hitInfo;
  196. if (Physics.Raycast(ray, out hitInfo, 100))
  197. {
  198. targetPosition = hitInfo.point;
  199. }
  200. else
  201. {
  202. targetPosition = transform.position + transform.forward * 100f;
  203. }
  204. Vector3 v3 = bowCameraDoublePlayer.bowCameraFixed.camera.WorldToViewportPoint(targetPosition) - centerPoint;
  205. v3.x *= parentRTF.rect.width * 0.5f;
  206. v3.y *= parentRTF.rect.height;
  207. v3.z = 0;
  208. crosshairTran.localPosition = v3;
  209. action_UpdatePostionWhenFixedCamera?.Invoke();
  210. }
  211. public System.Action action_UpdatePostionWhenFixedCamera;
  212. //设置准心显示或者隐藏
  213. void SetVisiable(bool value)
  214. {
  215. if (this.visiable == value) return;
  216. this.visiable = value;
  217. crosshairTran.GetComponent<Image>().enabled = this.visiable;
  218. }
  219. /// <summary>
  220. /// 同步GetOnlyShow
  221. /// </summary>
  222. public void SyncVisiable()
  223. {
  224. this.visiable = GetOnlyShow();
  225. crosshairTran.GetComponent<Image>().enabled = this.visiable;
  226. }
  227. public void openDisplayToCenter()
  228. {
  229. if (GetPlayerIndex() == 0)
  230. {
  231. crosshairTran.transform.localPosition = new Vector3(0.25f * parentRTF.rect.width, -parentRTF.rect.height * 0.5f, 0);
  232. }
  233. else if (GetPlayerIndex() == 1)
  234. {
  235. crosshairTran.transform.localPosition = new Vector3(0.25f * parentRTF.rect.width, -parentRTF.rect.height * 0.5f, 0);
  236. }
  237. }
  238. #endregion
  239. }