AimCrossHair.cs 8.6 KB

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