目次

はじめに

Leap Motion を Unity で使う最初の一歩として Unity Core Package 付属のサンプルと最小サンプルの 2 つを実行してみましょう。特に最小サンプルは Unity 上で Leap Motion からデータを取得する手順がよく分かると思います。

Leap Motion を置く位置

公式ではデスクトップの場合は緑の面が自分側、センサー部分が上です。下から手をセンシングする形です。しかし、割とどんな風に置いても認識はしますね。用途に応じた最適な位置を模索すると良いと思います。

参考:https://www.leapmotion.com/setup/desktop/windows

まずはサンプルを動かす

下記の手順で特に嵌りはなくサンプルの実行ができました。

  1. Leap_Motion_Core_Assets_4.3.3.unitypackage をダウンロードしてインポート
  2. Assets/LeapMotion/Examples/Leap Hands Demo (Desktop).unity を開く
  3. プレイをクリックし、Leap Motion が手を認識すると画面上に手が描画されます
  4. 以上

5 つの球を指と連動して動かすサンプル

参考サイト 1. にある最小サンプルで 5 つの球を動かす手順です。可読性を良くするために改変したものを張っておきます。

SimpleLeapMotion.cs

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

public class SimpleLeapMotion : MonoBehaviour
{
    Controller controller;
    public GameObject[] FingerObjects;

    void Start()
    {
        controller = new Controller();
    }

    void OnApplicationQuit()
    {
        if (controller != null)
        {
            controller.StopConnection();
            controller.Dispose();
            controller = null;
        }
    }

    void Update()
    {
        Frame frame = controller.Frame();
        List<Hand> hands = frame.Hands;

        foreach(var hand in hands)
        {
            List<Finger> fingers = hand.Fingers;
            for (int idxFinger = 0; idxFinger < fingers.Count; ++idxFinger)
            {
                Finger finger = fingers[idxFinger];
                Vector fingerPos = finger.TipPosition;

                var fingerObj = FingerObjects[idxFinger];
                fingerObj.transform.position = UnityVectorExtension.ToVector3(fingerPos) / 10;
            }
        }
    }
}

以下手順です。GameObject の名前は何でもいいです。

  1. 空の GameObject を作成し LeapMotion にリネーム
  2. LeapMotion に SimpleLeapMotion.cs をアタッチ
  3. 球を 5 個作成し、finger0 ~ 4 にリネーム
  4. SimpleLeapMotion を選択し、Finger objects の size を 5 に変更
  5. Finger objects に finger0 ~ 4 をアサイン
  6. プレイしセンサーに手をかざすと下記の様に球が動きます
  7. 以上

 

参考

  1. https://qiita.com/BobZombie/items/3c4234e23afd8b8c05d1
    Leap Motion を Unity 上で使うための最小サンプル有り
  2. https://qiita.com/afjk/items/e2c9617e937528feba6e#_reference-763434dc21485fa31833
    Unity ちゃんの手を動かすデモの紹介