using System; using System.Collections; using Leap.Unity.Attributes; using UnityEngine; namespace Leap.Unity { public class PalmDirectionDetector : Detector { private void OnValidate() { if (this.OffAngle < this.OnAngle) { this.OffAngle = this.OnAngle; } } private void Awake() { this.watcherCoroutine = this.palmWatcher(); } private void OnEnable() { base.StartCoroutine(this.watcherCoroutine); } private void OnDisable() { base.StopCoroutine(this.watcherCoroutine); this.Deactivate(); } private IEnumerator palmWatcher() { for (;;) { if (this.HandModel != null) { Hand hand = this.HandModel.GetLeapHand(); if (hand != null) { Vector3 normal = hand.PalmNormal.ToVector3(); float num = Vector3.Angle(normal, this.selectedDirection(hand.PalmPosition.ToVector3())); if (num <= this.OnAngle) { this.Activate(); } else if (num > this.OffAngle) { this.Deactivate(); } } } yield return new WaitForSeconds(this.Period); } yield break; } private Vector3 selectedDirection(Vector3 tipPosition) { switch (this.PointingType) { case PointingType.RelativeToCamera: return Camera.main.transform.TransformDirection(this.PointingDirection); case PointingType.RelativeToHorizon: { float y = Camera.main.transform.rotation.eulerAngles.y; Quaternion rotation = Quaternion.AngleAxis(y, Vector3.up); return rotation * this.PointingDirection; } case PointingType.RelativeToWorld: return this.PointingDirection; case PointingType.AtTarget: return this.TargetObject.position - tipPosition; default: return this.PointingDirection; } } [Tooltip("The interval in seconds at which to check this detector's conditions.")] public float Period = 0.1f; [AutoFind(AutoFindLocations.Parents)] [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")] public IHandModel HandModel; [Tooltip("The target direction.")] public Vector3 PointingDirection = Vector3.forward; [Tooltip("How to treat the target direction.")] public PointingType PointingType = PointingType.RelativeToHorizon; [Tooltip("A target object(optional). Use PointingType.AtTarget")] public Transform TargetObject; [Tooltip("The angle in degrees from the target direction at which to turn on.")] [Range(0f, 360f)] public float OnAngle = 45f; [Tooltip("The angle in degrees from the target direction at which to turn off.")] [Range(0f, 360f)] public float OffAngle = 65f; private IEnumerator watcherCoroutine; } }