LoginSignup
11
11

More than 5 years have passed since last update.

LeapMotionを使ってLive2Dのキャラクタに触れてみる

Last updated at Posted at 2015-05-09

LeapMotionを使ってLive2Dのキャラクタに触れてみる

スクリーンショット 2015-05-09 13.02.26.png

概要

Live2D SDKに入っているSampleApp1にLeapMotionをつなげて機能を追加してみます.
Leap Motionの値を利用してキャラにタッチしてみます.(表情も変わる

開発環境

  • Live2D_SDK_Unity_2.0.08_1_jp
  • Unity 5.0.1f1

作り方

準備

SDK(Live2D_SDK_Unity_2.0.08_1とか)をダウンロード
http://www.live2d.com/download
SDK試用申込をしてダウンロードしてくる

モデルを描画して動作を確認
SDKをダウンロードしたらsample/Simpleとかを開いて動作を確認する
今回はsample/SampleApp1を使う

Leap Motionで取得した値をモデルに反映させる

LeapMotionをsetup
LeapMotion_CoreAsset_2_2_4.unitypackageをインポート
Projectに追加されたLeapMotion/Prefabs/HandControllerをHierarchyに追加

HandControllerのPositionをX=0,Y=-10,Z=-7に設定
Hand Movement Scale X=5,Y=3,Z=3に設定

実行してモデルの前に手が表示されていることを確認

TestScript.csをプロジェクトにコピー

test.cs
using UnityEngine;
using System.Collections;
using Leap;

public class TestScript : MonoBehaviour {
    Controller controller;

    void Start ()
    {
        controller = new Controller();
        //gesture
        controller.EnableGesture(Gesture.GestureType.TYPECIRCLE);
        controller.EnableGesture(Gesture.GestureType.TYPEKEYTAP);
        controller.EnableGesture(Gesture.GestureType.TYPESCREENTAP);
        controller.EnableGesture(Gesture.GestureType.TYPE_SWIPE);
    }

    void Update ()
    {
        Frame frame = controller.Frame();
        // do something with the tracking data in the frame...
        if (! frame.Hands.IsEmpty) {
            var hand = frame.Hands[0];
            var gestures = frame.Gestures();

            for (int i = 0; i < hand.Fingers.Count; i++) {
                //人差し指
                if (hand.Fingers[i].Type() == Finger.FingerType.TYPE_INDEX){
                    //Debug.Log (hand.Fingers[i].TipPosition.x);
                    var fingerPosition = hand.Fingers[i].TipPosition;

                    for ( int gi = 0 ; gi < gestures.Count ; gi++ ) {
                        // ジェスチャー結果取得&表示
                        Gesture gesture = gestures[gi];
                        switch ( gesture.Type ) {
                        case Gesture.GestureType.TYPECIRCLE:
                            var circleGesture = new CircleGesture(gesture);
                            Debug.Log("Circle");
                            if(circleGesture.State == Gesture.GestureState.STATEUPDATE){
                                //そっちを向かせる(ドラッグと同じ
                                LAppLive2DManager.Instance.TouchesMoved(new Vector3((fingerPosition.x-20.0f+(UnityEngine.Screen.width/2)),(fingerPosition.y*2 - (UnityEngine.Screen.height)/2),0));
                            }
                            break;
                        case Gesture.GestureType.TYPEKEYTAP:
                            var keytapGesture = new KeyTapGesture(gesture);
                            Debug.Log("KeyTap");
                            if(keytapGesture.State == Gesture.GestureState.STATESTOP){
                                //Debug.Log (fingerPosition.x+","+fingerPosition.y+","+fingerPosition.z);
                                //20.0fはx軸に対するずれ軽減用
                                LAppLive2DManager.Instance.TouchesEnded(new Vector3((fingerPosition.x-20.0f+(UnityEngine.Screen.width/2)),(fingerPosition.y*2 - (UnityEngine.Screen.height)/2),0));
                            }
                            break;
                        case Gesture.GestureType.TYPESCREENTAP:
                            var screenTapGesture = new ScreenTapGesture(gesture);
                            Debug.Log("ScreenTap");
                            break;
                        case Gesture.GestureType.TYPE_SWIPE:
                            var swipeGesture = new SwipeGesture(gesture);
                            Debug.Log("Swipe");
                            //LAppLive2DManager.Instance.ChangeModel(); //モデル変更
                            break;
                        default:
                            break;
                        }
                    }

                }
            }

        }
    }
}

MainCameraとかにアタッチして実行

11
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11