Unity Leap Motion を使ってみる
目次
はじめに
Leap Motion を Unity で使う最初の一歩として Unity Core Package 付属のサンプルと最小サンプルの 2 つを実行してみましょう。特に最小サンプルは Unity 上で Leap Motion からデータを取得する手順がよく分かると思います。
Leap Motion を置く位置
公式ではデスクトップの場合は緑の面が自分側、センサー部分が上です。下から手をセンシングする形です。しかし、割とどんな風に置いても認識はしますね。用途に応じた最適な位置を模索すると良いと思います。
参考:https://www.leapmotion.com/setup/desktop/windows
まずはサンプルを動かす
下記の手順で特に嵌りはなくサンプルの実行ができました。
- Leap_Motion_Core_Assets_4.3.3.unitypackage をダウンロードしてインポート
- Assets/LeapMotion/Examples/Leap Hands Demo (Desktop).unity を開く
- プレイをクリックし、Leap Motion が手を認識すると画面上に手が描画されます
- 以上
5 つの球を指と連動して動かすサンプル
参考サイト 1. にある最小サンプルで 5 つの球を動かす手順です。可読性を良くするために改変したものを張っておきます。
SimpleLeapMotion.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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 の名前は何でもいいです。
- 空の GameObject を作成し LeapMotion にリネーム
- LeapMotion に SimpleLeapMotion.cs をアタッチ
- 球を 5 個作成し、finger0 ~ 4 にリネーム
- SimpleLeapMotion を選択し、Finger objects の size を 5 に変更
- Finger objects に finger0 ~ 4 をアサイン
- プレイしセンサーに手をかざすと下記の様に球が動きます
- 以上
参考
- https://qiita.com/BobZombie/items/3c4234e23afd8b8c05d1
Leap Motion を Unity 上で使うための最小サンプル有り - https://qiita.com/afjk/items/e2c9617e937528feba6e#_reference-763434dc21485fa31833
Unity ちゃんの手を動かすデモの紹介
コメントを残す