本日は Unity(hololens) の技術調査枠です。
下記の記事を参考に、hololens での手の位置検出について試してみます。
qiita.com
まずは新規プロジェクトを作成します。
今回のプロジェクトは hololens 上での確認が必須になるので、HoloToolKit をインポートして
hololens 向けプロジェクトに変更しておきます。
動かすオブジェクトのサンプルとして Sphere オブジェクトを配置します。
参考記事の HandPosition.cs を元に Sphere オブジェクトを掴んで運ぶスクリプトを作成します。
・HandPosition.cs(改二)
using UnityEngine; using System.Collections; using UnityEngine.VR.WSA.Input; public class HandPosition : MonoBehaviour { public GameObject HandObject; private bool HandFlg; void Start() { HandFlg = false; InteractionManager.SourceUpdated += InteractionManager_SourceUpdated; InteractionManager.SourceLost += InteractionManager_SourceLost; InteractionManager.SourcePressed += InteractionManager_SourcePressed; InteractionManager.SourceReleased += InteractionManager_SourceReleased; } void InteractionManager_SourceUpdated(InteractionSourceState state) { Vector3 handPosition; if (HandFlg == true) { if (state.source.kind == InteractionSourceKind.Hand && state.properties.location.TryGetPosition(out handPosition)) { HandObject.transform.position = handPosition; } } } void InteractionManager_SourceLost(InteractionSourceState state) { HandFlg = false; } void InteractionManager_SourcePressed(InteractionSourceState state) { HandFlg = true; } void InteractionManager_SourceReleased(InteractionSourceState state) { HandFlg = false; } }
ゲーム内のオブジェクトに作成したスクリプトを適用します。
その後、Sphere オブジェクトをドラック対象のオブジェクトとして設定します。
アプリをビルドして hololens 上で動作してみます。
指刺す動作でドラッグ開始、離す動作でドラッグが終了します。
視線ではなく手の動きを検知しているため、例えば、オブジェクトを掴んで移動して、好きなところに置くといった動作が自然にできます。
色々と活用できそうですね。