Leap MotionでMMDモデルに触れたことを検出する
- 2016.02.28
- Leap Motion Unity
前回の記事でLeap Motionを使ってMMDモデルに触れるようになりましたので、次はモデルに触れたことを検出できるようにしてみます。
まずは、下記のスクリプトを用意します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using UnityEngine; using System.Collections; public class ModelTrigger: MonoBehaviour { private GameObject controller = null; private string[] touchInfo=new string[3]; // Use this class initialization void Start () { controller = GameObject.Find("TriggerController"); } // Update is called once per frame void Update () { } void OnTriggerEnter(Collider other){ touchInfo[0]=transform.root.gameObject.name; touchInfo[1]=transform.parent.gameObject.name; touchInfo[2]=other.gameObject.name; controller.SendMessage("Touch",touchInfo); } void OnTriggerExit(Collider other) { //Debug.Log(other.gameObject); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using UnityEngine; using System.Collections; public class TriggerController : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void Touch(string[] touchInfo){ Debug.Log(touchInfo[0]+","+touchInfo[1]+","+touchInfo[2]); } } |
次に、triggerControllerをLeap Motionの手のモデルのBoneにアタッチし、そのモデルのPrefabをHandControllerにアタッチし直します。
最後に、空のGameObjectを作成し、ModelTriggerのスクリプトをアタッチすれば完了です。
上のスクリプトでは、左右どちらの手のどの指がどのオブジェクトに触れたのかを、コンソール上に表示するようにしています。
これを応用すれば、特定の部位に触れた時に、所定のリアクションをさせることも出来ると思います。
-
前の記事
ネットワーク機能を使ったアプリケーション間の通信 (Unity) 2016.02.14
-
次の記事
雑誌(VRFREEK)の付録でVR体験 2016.03.21