using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; //首先判断手和头部之间的距离,距离到一定距离时,在头部发出一条射线,射线碰到的菜单的按钮变成hover状态 public class Menu : MonoBehaviour { public GameObject leftHand; public GameObject head; public GameObject mouseIcon; public Image mouseIcon_Image; public Collider menuCollider; public float y; public UnityEvent shakeEvent; private Button menuButton; private RaycastHit menuMouse; //每隔0.3秒进行一次震动长度检测,超过一定的数值便触发该事件 private Vector3 shakePoint; private float shakeDistance; private int shakeTimer; // Use this for initialization void Start () { shakeTimer = 30; shakeDistance = 0f; } // Update is called once per frame void Update () { lookMenu(); } void lookMenu() { Vector3 startRayPoint = head.transform.position - new Vector3(0f, y, 0f); if (Physics.Raycast(startRayPoint, head.transform.forward,out menuMouse, 1)) { //print(menuMouse.collider); Debug.DrawRay(startRayPoint, head.transform.forward * 1f, Color.red); if (menuMouse.collider == menuCollider) { mouseIcon_Image.transform.position = menuMouse.point; //print("target on"); }else { //print("target lost"); } } //甩手方法 if (shakeTimer > 0) { shakeTimer -= 1; shakeDistance = shakeDistance + Mathf.Abs(shakePoint.x - leftHand.transform.position.x) + Mathf.Abs(shakePoint.y - leftHand.transform.position.y) + Mathf.Abs(shakePoint.y - leftHand.transform.position.y); shakePoint = leftHand.transform.position; } else { shakeTimer = 30; shakeDistance = 0f; } if (shakeDistance > 1.5f) { shakeEvent.Invoke(); shakeDistance = 0f; } } void clickMenu() { } void onPressMenu() { } void onReleaseMenu() { } public void shakeMenu() { print("shakeEvent!!!"); } }