ShootingEvent.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SmartBowSDK;
  5. using System;
  6. using UnityEngine.UI;
  7. public class ShootingEvent : MonoBehaviour
  8. {
  9. private SmartBowHelper smartBowHelper;
  10. /// <summary>
  11. /// 瞄准器,基于其位置发射射线,射线的hitpoint转屏幕坐标,该坐标即准心坐标
  12. /// </summary>
  13. public Transform aimTransform;
  14. /// <summary>
  15. /// 是否限制瞄准器在屏幕范围内
  16. /// </summary>
  17. public bool limitAimInScrren;
  18. private RectTransform _rectTransform;
  19. private RectTransform _canvasRectTransform;
  20. bool shooting = false;
  21. void Awake()
  22. {
  23. _rectTransform = GetComponent<RectTransform>();
  24. _canvasRectTransform = GetComponentInParent<Canvas>().GetComponent<RectTransform>();
  25. }
  26. void Start()
  27. {
  28. //#if UNITY_ANDROID
  29. // //smartBowHelper = new SmartBowHelper();
  30. // //SmartBowHelper.GetInstance().StartShootingSensor();
  31. // //SmartBowHelper.GetInstance().StartMagCalibration();
  32. // //SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress;
  33. // //SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate;
  34. // //SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress;
  35. // //SmartBowHelper.GetInstance().OnShooting += OnShooting;
  36. //#endif
  37. // InitForLimitBound();
  38. }
  39. void LateUpdate()
  40. {
  41. #if UNITY_ANDROID
  42. //if (limitAimInScrren) aimTransform.eulerAngles = LimitAngles(aimTransform.eulerAngles);
  43. //UpdateCrossHairPosition();
  44. #else
  45. Vector3 pos = new Vector3(Input.mousePosition.x - Screen.width * 0.5f, Input.mousePosition.y - Screen.height * 0.5f, 0);
  46. _rectTransform.localPosition = pos;
  47. #endif
  48. }
  49. private Vector2 _point;
  50. public void OnPositionUpdate(Vector2 point)
  51. {
  52. _point = point;
  53. //Debug.LogWarning($"OnPositionUpdate point={point}");
  54. var viewPoint = Camera.main.ScreenToViewportPoint(point);
  55. var width = _canvasRectTransform.sizeDelta.x;
  56. var height = _canvasRectTransform.sizeDelta.y;
  57. point.x = width * viewPoint.x;
  58. point.y = height * viewPoint.y;
  59. var uiPos = new Vector3(point.x - width * 0.5f, point.y - height * 0.5f, -275f);
  60. //Debug.LogWarning($"uiPos={uiPos}");
  61. _rectTransform.localPosition = uiPos;//准心UI位置
  62. }
  63. //射箭的监听
  64. public void OnShooting()
  65. {
  66. shooting = true;
  67. var worldPoint = Camera.main.ScreenToWorldPoint(_point);
  68. //var rayGo = new GameObject("Ray");
  69. //rayGo.transform.position = worldPoint;
  70. //var ray = Camera.main.ScreenPointToRay(_point);
  71. UpdateCrossHairPosition();
  72. }
  73. /// <summary>
  74. /// 判断是否打中UI按钮
  75. /// </summary>
  76. /// <returns></returns>
  77. public bool ShootUIBtn()
  78. {
  79. var ray = new Ray(transform.position, transform.forward);
  80. RaycastHit hitInfo;
  81. if (Physics.Raycast(ray, out hitInfo))
  82. {
  83. var go = hitInfo.transform.gameObject;
  84. if (go.GetComponent<Button>())
  85. {
  86. if (go.name == "BtnRestart")
  87. GeneratingTarget.gm.OnRestart();
  88. else if (go.name == "BtnNext")
  89. GeneratingTarget.gm.OnBtnNext();
  90. else if (go.name == "BtnLast")
  91. GeneratingTarget.gm.OnBtnLast();
  92. }
  93. }
  94. return false;
  95. }
  96. private void Update()
  97. {
  98. Ray ray = new Ray(transform.position, transform.forward);
  99. Debug.DrawRay(ray.origin, ray.direction * 5000f, Color.red);
  100. }
  101. void UpdateCrossHairPosition()
  102. {
  103. var ray = new Ray(transform.position, transform.forward);
  104. RaycastHit hitInfo;
  105. Vector3 hitPoint;
  106. //Debug.DrawLine(aimTransform.position, aimTransform.forward, Color.blue);
  107. //Debug.LogWarning($"发射射线");
  108. bool missed = false;
  109. if (shooting)
  110. missed = true;
  111. if (Physics.Raycast(ray, out hitInfo))
  112. {
  113. if (GeneratingTarget.gm.stop)
  114. return;
  115. Debug.LogWarning($"打中目标");
  116. hitPoint = hitInfo.point;
  117. if (shooting)
  118. {
  119. if (hitInfo.transform.name.Contains("target"))
  120. {
  121. //射中靶子 这里写入射中靶子的逻辑
  122. missed = false;
  123. hitInfo.transform.GetComponent<SpineAnimationLoader>().CalculateScore(hitInfo.point);
  124. }
  125. }
  126. }
  127. if (missed)
  128. GeneratingTarget.gm.ShowMiss(transform.position);
  129. //hitPoint = aimTransform.position + aimTransform.forward * 100f;
  130. shooting = false;
  131. //Vector2 viewPoint = Camera.main.WorldToViewportPoint(hitPoint);
  132. //_rectTransform.localPosition = (viewPoint - Vector2.one * 0.5f) * _canvasRectTransform.rect.size;
  133. }
  134. ////短按功能键的监听
  135. ////void OnFunctionKeyPress()
  136. ////{
  137. //// Debug.Log("射击");
  138. //// shooting = true;
  139. ////}
  140. void OnFunctionKeyLongPress()
  141. {
  142. //长按功能键视角归位//以测试情况看是否用长按功能键这个方式来校准视角
  143. //smartBowHelper.ResetAim();
  144. }
  145. //void OnRotationUpdate(Quaternion rotation)
  146. //{
  147. // aimTransform.localRotation = rotation;
  148. //}
  149. #region 限制其显示范围不超出屏幕
  150. private float _screenWidthHalf;
  151. private float _screenHeightHalf;
  152. private float _screenNormal;
  153. private float _rangeRotateY;
  154. private float _rangeRotateX;
  155. /// <summary>
  156. /// 标记是否出界(-1:未出界;0:左边出界;1:右边出界;2:上边出界;3:下边出界;)
  157. /// </summary>
  158. [NonSerialized] public int outBoundIndex = -1;
  159. void InitForLimitBound()
  160. {
  161. Camera camera = Camera.main;
  162. _rangeRotateX = camera.fieldOfView / 2;
  163. _screenWidthHalf = Screen.width / 2f;
  164. _screenHeightHalf = Screen.height / 2f;
  165. _screenNormal = _screenHeightHalf / Mathf.Tan(_rangeRotateX / 180f * Mathf.PI);
  166. _rangeRotateY = Mathf.Atan(_screenWidthHalf / _screenNormal) / Mathf.PI * 180f;
  167. }
  168. Vector3 LimitAngles(Vector3 angles)
  169. {
  170. //Unity默认范围(0,360)格式化为自定义范围(-180,180)
  171. angles.y = angles.y > 180 ? angles.y - 360 : angles.y;
  172. angles.x = angles.x > 180 ? angles.x - 360 : angles.x;
  173. //记录原来的角度
  174. float angleY = angles.y;
  175. float angleX = angles.x;
  176. //记录是否出界
  177. if (angleY < -_rangeRotateY) outBoundIndex = 0;
  178. else if (angleY > _rangeRotateY) outBoundIndex = 1;
  179. else if (angleX < -_rangeRotateX) outBoundIndex = 2;
  180. else if (angleX > _rangeRotateX) outBoundIndex = 3;
  181. else outBoundIndex = -1;
  182. //返回限制边界后的角度
  183. angles.y = Mathf.Clamp(angleY, -_rangeRotateY, _rangeRotateY);
  184. float rayLen = _screenNormal / Mathf.Cos(Mathf.Abs(angles.y) / 180 * Mathf.PI);
  185. float maxRangeRotateX = Mathf.Atan(_screenHeightHalf / rayLen) / Mathf.PI * 180f;
  186. if (Mathf.Abs(angleX) > Mathf.Abs(maxRangeRotateX)) angles.x = maxRangeRotateX * (angleX > 0 ? 1 : -1);
  187. return angles;
  188. }
  189. #endregion
  190. }