| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using SmartBowSDK;
- using System;
- using UnityEngine.UI;
- public class ShootingEvent : MonoBehaviour
- {
- private SmartBowHelper smartBowHelper;
- /// <summary>
- /// 瞄准器,基于其位置发射射线,射线的hitpoint转屏幕坐标,该坐标即准心坐标
- /// </summary>
- public Transform aimTransform;
- /// <summary>
- /// 是否限制瞄准器在屏幕范围内
- /// </summary>
- public bool limitAimInScrren;
- private RectTransform _rectTransform;
- private RectTransform _canvasRectTransform;
- bool shooting = false;
- void Awake()
- {
- _rectTransform = GetComponent<RectTransform>();
- _canvasRectTransform = GetComponentInParent<Canvas>().GetComponent<RectTransform>();
- }
- 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();
- }
- /// <summary>
- /// 判断是否打中UI按钮
- /// </summary>
- /// <returns></returns>
- public bool ShootUIBtn()
- {
- var ray = new Ray(transform.position, transform.forward);
- RaycastHit hitInfo;
- if (Physics.Raycast(ray, out hitInfo))
- {
- var go = hitInfo.transform.gameObject;
- if (go.GetComponent<Button>())
- {
- if (go.name == "BtnRestart")
- GeneratingTarget.gm.OnRestart();
- else if (go.name == "BtnNext")
- GeneratingTarget.gm.OnBtnNext();
- else if (go.name == "BtnLast")
- GeneratingTarget.gm.OnBtnLast();
- }
- }
- return false;
- }
- 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<SpineAnimationLoader>().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;
- /// <summary>
- /// 标记是否出界(-1:未出界;0:左边出界;1:右边出界;2:上边出界;3:下边出界;)
- /// </summary>
- [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
- }
|