找回密码
 立即注册
查看: 302|回复: 0

LeapMotion在unity中保姆级使用教程

[复制链接]
发表于 2022-4-28 13:34 | 显示全部楼层 |阅读模式

一、插件

1、下载资源包,包括:Core为核心引擎,Interaction Engine为实现虚拟物体交互的插件,hands提供手势渲染等。
Ultraleap Plugin for Unity — Ultraleap for Developers (leapmotion.com)

https://developer.leapmotion.com/unity


2、unity中导入
3、安装Magic Leap XR Plugin


4、准备完成

二、场景创建

1、在自己工程的场景需要添加以下,一个个说


位置如下


2、对于LeapHanController模块,其中的Hand Model Manager这样添,该拖的物体拖进来


这个脚本能选择使用的是AR还是电脑


3、 就这么简单,基本环境就搭建好了。

三、使用方法

1、创建一个脚本,挂载在场景里
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //引用
  5. using Leap;
  6. using Leap.Unity;
  7. public class HandControl : MonoBehaviour
  8. {
  9.     LeapProvider provider;
  10.     public HandModelBase leftHandModel;//左手
  11.     public HandModelBase rightHandModel;//右手
  12.     private const float rotate_sensitive = 1500f;  //旋转灵敏度
  13.     private const float displacement_sensitive = 0.015f; //位移灵敏度
  14.     private const float rotate_initial_value = 0f;  //旋转初始位置值
  15.     float shake = 0;
  16.     /// <summary>
  17.     /// 判断条件  尽量勿动
  18.     /// </summary>
  19.     const float smallestVelocity = 0.1f;
  20.     const float deltaVelocity = 0.000001f;
  21.     const float deltaCloseFinger = 0.06f;
  22.     void Start()
  23.     {
  24.         provider = FindObjectOfType<LeapProvider>() as LeapProvider;
  25.     }
  26.     void Update()
  27.     {
  28.         jjj();
  29.     }
  30.     public void jjj()
  31.     {
  32.         Frame frame = provider.CurrentFrame;
  33.         foreach (Hand hand in frame.Hands)
  34.         {
  35.             if (hand.IsLeft && !hand.IsRight)
  36.             {
  37.                 if (isOpenFullHand(hand))
  38.                 {
  39.                     //Debug.Log("检测到手啦");
  40.                     if (isMoveRight(hand))
  41.                     {
  42.                         shake += Time.deltaTime;//计手移动的时间
  43.                         if (shake > 0.8f)
  44.                         {
  45.                             shake = 0;//时间大于*f后,时间置零,避免一直触发
  46.                             Debug.Log("左掌向右");
  47.                         }
  48.                     }
  49.                 }
  50.                 if (isCloseHand(hand))
  51.                 {
  52.                     if (isMoveRight(hand))
  53.                     {
  54.                         Debug.Log("左拳向右");
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60.     /// <summary>
  61.     /// 定义手势的基础类型
  62.     /// </summary>
  63.     /// <param name="hand"></param>
  64.     /// <returns></returns>
  65.     protected bool isMoveRight(Hand hand)// 手划向右
  66.     {
  67.         return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);//x,y,z控制三维轴,±控制轴方向
  68.     }
  69.     protected bool isMoveLeft(Hand hand)   // 手划向左
  70.     {
  71.         //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)  判断hand是否禁止
  72.         return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
  73.     }
  74.     protected bool isMoveup(Hand hand)   // 手划向上
  75.     {
  76.         //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)  判断hand是否禁止
  77.         return hand.PalmVelocity.y < deltaVelocity && !isStationary(hand);
  78.     }
  79.     protected bool isMovedown(Hand hand)   // 手划向下
  80.     {
  81.         //x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)  判断hand是否禁止
  82.         return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
  83.     }
  84.     protected bool isStationary(Hand hand)// 固定不动的
  85.     {
  86.         return hand.PalmVelocity.Magnitude < smallestVelocity;
  87.     }
  88.     protected bool isCloseHand(Hand hand)     //是否握拳
  89.     {
  90.         List<Finger> listOfFingers = hand.Fingers;
  91.         int count = 0;
  92.         for (int f = 0; f < listOfFingers.Count; f++)
  93.         { //循环遍历所有的手~~
  94.             Finger finger = listOfFingers[f];
  95.             if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger)    // Magnitude  向量的长度 。是(x*x+y*y+z*z)的平方根。    //float deltaCloseFinger = 0.05f;
  96.             {
  97.                 count++;
  98.                 //  if (finger.Type == Finger.FingerType.TYPE_THUMB)
  99.                 //  Debug.Log ((finger.TipPosition - hand.PalmPosition).Magnitude);
  100.             }
  101.         }
  102.         return (count == 5);
  103.     }
  104.     protected bool isOpenFullHand(Hand hand)         //手掌全展开~
  105.     {
  106.         //Debug.Log (hand.GrabStrength + " " + hand.PalmVelocity + " " + hand.PalmVelocity.Magnitude);
  107.         return hand.GrabStrength == 0;
  108.     }
  109. }
复制代码
其它手势基础,可以参考这个:
LeapMotion初步学习_alnh9788的博客-CSDN博客

https://blog.csdn.net/alnh9788/article/details/101463386
LEAPMotion VR 各种手势的判断~_miccall的博客-CSDN博客_leapmotion 手势识别

https://blog.csdn.net/qq_31411825/article/details/54773801Leapmotion 左右上下前后挥动手势设计,动态手势_moonlightpeng的博客-CSDN博客

https://blog.csdn.net/moonlightpeng/article/details/80191468?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1.no_search_link&spm=1001.2101.3001.4242.2 判断每个手指的动作,参考这个:
unity + leapMotion 手势识别入门教程__Afra 的博客-CSDN博客_leapmotion手势识别

https://blog.csdn.net/qq_39097425/article/details/84027730?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~default-1.essearch_pc_relevant
2、就可以用了,Debug的一些语句就能触发了
3、 控制捏动作的脚本


这个圈圈判断是否捏住


4、抓取物体
在需要交互的物体上加上Rigidbody和Interaction Behaviour两个组件

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-3 16:54 , Processed in 0.136207 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表