ShootingEvent.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. private void Update()
  74. {
  75. Ray ray = new Ray(transform.position, transform.forward);
  76. Debug.DrawRay(ray.origin, ray.direction * 5000f, Color.red);
  77. }
  78. void UpdateCrossHairPosition()
  79. {
  80. var ray = new Ray(transform.position, transform.forward);
  81. RaycastHit hitInfo;
  82. Vector3 hitPoint;
  83. //Debug.DrawLine(aimTransform.position, aimTransform.forward, Color.blue);
  84. //Debug.LogWarning($"发射射线");
  85. bool missed = false;
  86. if (shooting)
  87. missed = true;
  88. if (Physics.Raycast(ray, out hitInfo))
  89. {
  90. if (GeneratingTarget.gm.stop)
  91. return;
  92. Debug.LogWarning($"打中目标");
  93. hitPoint = hitInfo.point;
  94. if (shooting)
  95. {
  96. if (hitInfo.transform.name.Contains("target"))
  97. {
  98. //射中靶子 这里写入射中靶子的逻辑
  99. missed = false;
  100. hitInfo.transform.GetComponent<SpineAnimationLoader>().CalculateScore(hitInfo.point);
  101. }
  102. }
  103. }
  104. if (missed)
  105. GeneratingTarget.gm.ShowMiss(transform.position);
  106. //hitPoint = aimTransform.position + aimTransform.forward * 100f;
  107. shooting = false;
  108. //Vector2 viewPoint = Camera.main.WorldToViewportPoint(hitPoint);
  109. //_rectTransform.localPosition = (viewPoint - Vector2.one * 0.5f) * _canvasRectTransform.rect.size;
  110. }
  111. ////短按功能键的监听
  112. ////void OnFunctionKeyPress()
  113. ////{
  114. //// Debug.Log("射击");
  115. //// shooting = true;
  116. ////}
  117. void OnFunctionKeyLongPress()
  118. {
  119. //长按功能键视角归位//以测试情况看是否用长按功能键这个方式来校准视角
  120. //smartBowHelper.ResetAim();
  121. }
  122. //void OnRotationUpdate(Quaternion rotation)
  123. //{
  124. // aimTransform.localRotation = rotation;
  125. //}
  126. #region 限制其显示范围不超出屏幕
  127. private float _screenWidthHalf;
  128. private float _screenHeightHalf;
  129. private float _screenNormal;
  130. private float _rangeRotateY;
  131. private float _rangeRotateX;
  132. /// <summary>
  133. /// 标记是否出界(-1:未出界;0:左边出界;1:右边出界;2:上边出界;3:下边出界;)
  134. /// </summary>
  135. [NonSerialized] public int outBoundIndex = -1;
  136. void InitForLimitBound()
  137. {
  138. Camera camera = Camera.main;
  139. _rangeRotateX = camera.fieldOfView / 2;
  140. _screenWidthHalf = Screen.width / 2f;
  141. _screenHeightHalf = Screen.height / 2f;
  142. _screenNormal = _screenHeightHalf / Mathf.Tan(_rangeRotateX / 180f * Mathf.PI);
  143. _rangeRotateY = Mathf.Atan(_screenWidthHalf / _screenNormal) / Mathf.PI * 180f;
  144. }
  145. Vector3 LimitAngles(Vector3 angles)
  146. {
  147. //Unity默认范围(0,360)格式化为自定义范围(-180,180)
  148. angles.y = angles.y > 180 ? angles.y - 360 : angles.y;
  149. angles.x = angles.x > 180 ? angles.x - 360 : angles.x;
  150. //记录原来的角度
  151. float angleY = angles.y;
  152. float angleX = angles.x;
  153. //记录是否出界
  154. if (angleY < -_rangeRotateY) outBoundIndex = 0;
  155. else if (angleY > _rangeRotateY) outBoundIndex = 1;
  156. else if (angleX < -_rangeRotateX) outBoundIndex = 2;
  157. else if (angleX > _rangeRotateX) outBoundIndex = 3;
  158. else outBoundIndex = -1;
  159. //返回限制边界后的角度
  160. angles.y = Mathf.Clamp(angleY, -_rangeRotateY, _rangeRotateY);
  161. float rayLen = _screenNormal / Mathf.Cos(Mathf.Abs(angles.y) / 180 * Mathf.PI);
  162. float maxRangeRotateX = Mathf.Atan(_screenHeightHalf / rayLen) / Mathf.PI * 180f;
  163. if (Mathf.Abs(angleX) > Mathf.Abs(maxRangeRotateX)) angles.x = maxRangeRotateX * (angleX > 0 ? 1 : -1);
  164. return angles;
  165. }
  166. #endregion
  167. }