贴几个直接能在Unity(2017.4.27f1)上测试的代码
1.测试手势的
1 using Leap; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEngine; 5 using Leap.Unity; 6 /// <summary> 7 /// 旋转/缩放/移动 8 /// </summary> 9 public class ControlAnimation : MonoBehaviour 10 { 11 12 LeapProvider provider; 13 public HandModelBase leftHandModel; 14 public HandModelBase rightHandModel; 15 private const float rotate_sensitive = 1500f; //旋转灵敏度 16 private const float displacement_sensitive = 0.015f; //位移灵敏度 17 private const float rotate_initial_value = 0f; //旋转初始位置值 18 19 /// <summary> 20 /// 判断条件 尽量勿动 21 /// </summary> 22 const float smallestVelocity = 0.1f; 23 const float deltaVelocity = 0.000001f; 24 const float deltaCloseFinger = 0.06f; 25 26 void Start() 27 { 28 provider = FindObjectOfType<LeapProvider>() as LeapProvider; 29 } 30 31 32 void Update() 33 { 34 Scale(); 35 Rotation(); 36 Position(); 37 } 38 39 40 /// <summary> 41 /// 缩小 42 /// </summary> 43 public void Scale() 44 { 45 Frame frame = provider.CurrentFrame; 46 foreach (Hand hand in frame.Hands) 47 { 48 if (isOpenFullHand(hand)) 49 { 50 Debug.Log("大"); 51 52 //Vector3 value = transform.localScale; 53 //value += new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f); 54 //// Debug.Log(value); 55 //transform.localScale = value; 56 transform.Translate(Vector3.forward * Time.deltaTime * 5); 57 } 58 if (isCloseHand(hand)) 59 { 60 //Debug.Log("小"); 61 //Vector3 value = transform.localScale; 62 //value -= new Vector3(value.x * 0.01f, value.y * 0.01f, value.z * 0.01f); 63 //// Debug.Log(value); 64 65 //transform.localScale = value; 66 } 67 } 68 } 69 70 71 /// <summary> 72 /// 旋转 73 /// </summary> 74 public void Rotation() 75 { 76 Frame frame = provider.CurrentFrame; 77 foreach (Hand hand in frame.Hands) 78 { 79 if (hand.IsLeft || hand.IsRight) 80 { 81 Vector3 value = transform.localEulerAngles; 82 value = new Vector3(/*hand.PalmPosition.y * rotate_sensitive + rotate_initial_value*/0, hand.PalmPosition.x * rotate_sensitive + rotate_initial_value, 0); 83 transform.localEulerAngles = value; 84 } 85 else 86 { 87 hand.PalmPosition.y = transform.localEulerAngles.x; 88 hand.PalmPosition.x = transform.localEulerAngles.y; 89 } 90 } 91 } 92 93 94 /// <summary> 95 /// 位移 96 /// </summary> 97 public void Position() 98 { 99 Frame frame = provider.CurrentFrame; 100 foreach (Hand hand in frame.Hands) 101 { 102 if (hand.IsLeft || hand.IsRight) 103 { 104 if (isMoveLeft(hand)) 105 { 106 transform.localPosition = new Vector3(hand.PalmPosition.y * displacement_sensitive, 0, 0) + transform.localPosition; 107 } 108 if (isMoveRight(hand)) 109 { 110 transform.localPosition = new Vector3(-hand.PalmPosition.y * displacement_sensitive, 0, 0) + transform.localPosition; 111 } 112 } 113 } 114 115 } 116 117 118 protected bool isMoveRight(Hand hand)// 手划向右边 119 { 120 Debug.Log("左滑"); 121 return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand); 122 } 123 124 125 protected bool isMoveLeft(Hand hand) // 手划向左边 126 { 127 Debug.Log("右滑"); 128 //x轴移动的速度 deltaVelocity = 0.7f isStationary (hand) 判断hand是否禁止 129 return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand); 130 } 131 132 protected bool isStationary(Hand hand)// 固定不动的 133 { 134 return hand.PalmVelocity.Magnitude < smallestVelocity; 135