using System.Collections; using System.Collections.Generic; using UnityEngine; using SmartBowSDK; using System; using UnityEngine.UI; public class ShootingEvent : MonoBehaviour { private SmartBowHelper smartBowHelper; /// /// 瞄准器,基于其位置发射射线,射线的hitpoint转屏幕坐标,该坐标即准心坐标 /// public Transform aimTransform; /// /// 是否限制瞄准器在屏幕范围内 /// public bool limitAimInScrren; private RectTransform _rectTransform; private RectTransform _canvasRectTransform; bool shooting = false; void Awake() { _rectTransform = GetComponent(); _canvasRectTransform = GetComponentInParent().GetComponent(); } void Start() { //#if UNITY_ANDROID // //smartBowHelper = new SmartBowHelper(); // //SmartBowHelper.GetInstance().StartShootingSensor(); // //SmartBowHelper.GetInstance().StartMagCalibration(); // //SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress; // //SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate; // //SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress; // //SmartBowHelper.GetInstance().OnShooting += OnShooting; //#endif // InitForLimitBound(); } void LateUpdate() { #if UNITY_ANDROID //if (limitAimInScrren) aimTransform.eulerAngles = LimitAngles(aimTransform.eulerAngles); //UpdateCrossHairPosition(); #else Vector3 pos = new Vector3(Input.mousePosition.x - Screen.width * 0.5f, Input.mousePosition.y - Screen.height * 0.5f, 0); _rectTransform.localPosition = pos; #endif } private Vector2 _point; public void OnPositionUpdate(Vector2 point) { _point = point; //Debug.LogWarning($"OnPositionUpdate point={point}"); var viewPoint = Camera.main.ScreenToViewportPoint(point); var width = _canvasRectTransform.sizeDelta.x; var height = _canvasRectTransform.sizeDelta.y; point.x = width * viewPoint.x; point.y = height * viewPoint.y; var uiPos = new Vector3(point.x - width * 0.5f, point.y - height * 0.5f, -275f); //Debug.LogWarning($"uiPos={uiPos}"); _rectTransform.localPosition = uiPos;//准心UI位置 } //射箭的监听 public void OnShooting() { shooting = true; var worldPoint = Camera.main.ScreenToWorldPoint(_point); //var rayGo = new GameObject("Ray"); //rayGo.transform.position = worldPoint; //var ray = Camera.main.ScreenPointToRay(_point); UpdateCrossHairPosition(); } private void Update() { Ray ray = new Ray(transform.position, transform.forward); Debug.DrawRay(ray.origin, ray.direction * 5000f, Color.red); } void UpdateCrossHairPosition() { var ray = new Ray(transform.position, transform.forward); RaycastHit hitInfo; Vector3 hitPoint; //Debug.DrawLine(aimTransform.position, aimTransform.forward, Color.blue); //Debug.LogWarning($"发射射线"); bool missed = false; if (shooting) missed = true; if (Physics.Raycast(ray, out hitInfo)) { if (GeneratingTarget.gm.stop) return; Debug.LogWarning($"打中目标"); hitPoint = hitInfo.point; if (shooting) { if (hitInfo.transform.name.Contains("target")) { //射中靶子 这里写入射中靶子的逻辑 missed = false; hitInfo.transform.GetComponent().CalculateScore(hitInfo.point); } } } if (missed) GeneratingTarget.gm.ShowMiss(transform.position); //hitPoint = aimTransform.position + aimTransform.forward * 100f; shooting = false; //Vector2 viewPoint = Camera.main.WorldToViewportPoint(hitPoint); //_rectTransform.localPosition = (viewPoint - Vector2.one * 0.5f) * _canvasRectTransform.rect.size; } ////短按功能键的监听 ////void OnFunctionKeyPress() ////{ //// Debug.Log("射击"); //// shooting = true; ////} void OnFunctionKeyLongPress() { //长按功能键视角归位//以测试情况看是否用长按功能键这个方式来校准视角 //smartBowHelper.ResetAim(); } //void OnRotationUpdate(Quaternion rotation) //{ // aimTransform.localRotation = rotation; //} #region 限制其显示范围不超出屏幕 private float _screenWidthHalf; private float _screenHeightHalf; private float _screenNormal; private float _rangeRotateY; private float _rangeRotateX; /// /// 标记是否出界(-1:未出界;0:左边出界;1:右边出界;2:上边出界;3:下边出界;) /// [NonSerialized] public int outBoundIndex = -1; void InitForLimitBound() { Camera camera = Camera.main; _rangeRotateX = camera.fieldOfView / 2; _screenWidthHalf = Screen.width / 2f; _screenHeightHalf = Screen.height / 2f; _screenNormal = _screenHeightHalf / Mathf.Tan(_rangeRotateX / 180f * Mathf.PI); _rangeRotateY = Mathf.Atan(_screenWidthHalf / _screenNormal) / Mathf.PI * 180f; } Vector3 LimitAngles(Vector3 angles) { //Unity默认范围(0,360)格式化为自定义范围(-180,180) angles.y = angles.y > 180 ? angles.y - 360 : angles.y; angles.x = angles.x > 180 ? angles.x - 360 : angles.x; //记录原来的角度 float angleY = angles.y; float angleX = angles.x; //记录是否出界 if (angleY < -_rangeRotateY) outBoundIndex = 0; else if (angleY > _rangeRotateY) outBoundIndex = 1; else if (angleX < -_rangeRotateX) outBoundIndex = 2; else if (angleX > _rangeRotateX) outBoundIndex = 3; else outBoundIndex = -1; //返回限制边界后的角度 angles.y = Mathf.Clamp(angleY, -_rangeRotateY, _rangeRotateY); float rayLen = _screenNormal / Mathf.Cos(Mathf.Abs(angles.y) / 180 * Mathf.PI); float maxRangeRotateX = Mathf.Atan(_screenHeightHalf / rayLen) / Mathf.PI * 180f; if (Mathf.Abs(angleX) > Mathf.Abs(maxRangeRotateX)) angles.x = maxRangeRotateX * (angleX > 0 ? 1 : -1); return angles; } #endregion }