⭐Unity LeapMotion与手的相关开发

LeapMotion 官方文档中文翻译帮助手册教程

Hand

一个Hand手对象表示了一个跟踪的手,一个手总是包含5个手指以及相关属性如:Direction,PalmPosition,和Basis(orientation).

lamPosition :手掌中心到Leap设备原点以毫米测量的距离

PalmVelocity :手掌移动的速度(以毫米每秒为单位)。

PalmNormal :一个向量,这个向量是垂直于手掌所形成的平面的。并且向量从手掌出来指向下。

Direction :一个向量,从手掌指向手指的方向。

判断是否是左手或者右手

if (currentFrame.Hands.Count > 0) // 判断当前帧中是否检测到有手的数量 > 0 。并且所有的手会在一个List数组中

{

for (int i = 0; i < currentFrame.Hands.Count; i++) // 如果大于0,就要遍历,因为2-4个手是可以检测到的

{

if (currentFrame.Hands[i].IsLeft) // 判断是左手

{

Debug.Log(currentFrame.Hands[i].ToString());

}

if (currentFrame.Hands[i].IsRight) // 判断是右手

{

Debug.Log(currentFrame.Hands[i].ToString());

}

}

}

判断手掌是否向上或者向下(基于手掌法线来进行的)

if (currentFrame.Hands[i].PalmNormal.y > 0) // PalmNormal手掌的法线量

{

Debug.Log("手掌向上");

}

以下代码供参考

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
using Leap;

public class LeapGestureTool1 : MonoBehaviour
{
    public static LeapGestureTool1 instance;
    private Controller leapController;
    public Camera leapCamera;

    public bool isQin = false;
    public float distance = 0;

    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        // 创建LeapMotion控制器实例
        leapController = new Controller();
    }

    void Update()
    {
        // 检查是否连接了Leap Motion控制器
        if (!leapController.IsConnected)
        {
            Debug.Log("Leap Motion未连接");
            return;
        }

        // 获取最新的帧数据
        Frame frame = leapController.Frame();

        // 获取第一个检测到的手部
        if (frame.Hands.Count > 0)
        {
            isQin = true;

            Hand hand = frame.Hands[0];

            // 获取手的位置
            Vector3 handPosition = hand.PalmPosition.ToVector3();

            // 获取相机的位置
            Vector3 cameraPosition = leapCamera.transform.position;

             计算人手与相机之间的距离
            // distance = Vector3.Distance(handPosition, cameraPosition);

            //Debug.Log("人手与Leap Motion相机的距离为:" + distance);

            Vector3 positionDifference = handPosition - cameraPosition;

            // 判断手在相机的前方还是后方
            //if (Vector3.Dot(leapCamera.transform.forward, positionDifference) > 0)
            //{
            //    Debug.Log("手在相机的前方");
            //}
            //else
            //{
            //    Debug.Log("手在相机的后方");
            //}

            // 判断手在相机的上方还是下方
            if (positionDifference.y > 0)
            {
                Debug.Log("手在相机的上方");
            }
            else
            {
                Debug.Log("手在相机的下方");
            }

            // 判断手在相机的左方还是右方
            Vector3 cameraRight = leapCamera.transform.right;
            if (Vector3.Dot(cameraRight, positionDifference) > 0)
            {
                Debug.Log("手在相机的右方");
            }
            else
            {
                Debug.Log("手在相机的左方");
            }
        }
        else
        {
            isQin = false;
            //Debug.Log("手不在检测范围");
        }
    }

}

你可能感兴趣的:(Unity,unity,游戏引擎)