LoginSignup
3
3

More than 5 years have passed since last update.

"Unity"+"leap motion" [練習九つ]: VRGUI Project

Last updated at Posted at 2015-12-09

VRGUI Project: 「Raycastについて」

Goal: 空間に浮かぶGUIを作る


  • Step 1: UnityでLeap Motionを使ってGesture Motion認識ができる。

  • Step 2: uGUIでメニューを作って空間に浮かべる

  • Step 3:メニュー選択。任意のGameObjectからRaycastを飛ばしてメニューを選択できる。選択状態で色を変える。できたら音も。

  • Step 4:メニューで選択した項目を実行。メニューに任意のアクションを割り当てられるように実行する。




Unityは "Raycast”という、ものがある。

仮想的な線を利用して衝突検出をする機能だ。

基本的には、Physics.Raycast という、関数を使う。

第五parameterまであるけど、第三のparameterまでは必須だ。第四と、第五のparameterは必須じゃない。

第一引数にはレイキャストの原点の位置、第二引数は方向、第三引数には衝突情報、第四引数には検知を行う距離、第五引数にはレイヤーマスクをとります。

UnityでLeapmotionの手の指からraycastが見えるように連取しましょう。


RaycastExample.cs
 RaycastHit hit;

 if(Physics.Raycast( transform.position,Vector3.right,out hit, 10 ))
 {
 hit.transform.gameObject;
 }

positionから右方向に10進んだ先までにオブジェクトがあればtrueが返される。

第三引数にoutで指定すると衝突したオブジェクトの情報が入る



指からdraw Raycastの例:
raycastVisualize.cs
using UnityEngine;
using System.Collections;
using Leap;

public class GetLeapFingers : MonoBehaviour 
{
    HandModel hand_model;
    Hand leap_hand;

    void Start() 
    {
        hand_model = GetComponent<HandModel>();
        leap_hand = hand_model.GetLeapHand();
        if (leap_hand == null) Debug.LogError("No leap_hand founded");
    }

    void Update() 
    {
        for (int i = 0; i < HandModel.NUM_FINGERS;i++)
        {
            FingerModel finger = hand_model.fingers[i];

            Debug.DrawRay(finger.GetTipPosition(), finger.GetRay().direction, Color.red); 
        }
    }
}

Screen Shot 2015-12-09 at 14.59.42.png


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

public class raycastTest : MonoBehaviour 
{
    HandModel handModel;
    Hand leapHand;

    // Use this for initialization
    void Start () 
    {
        handModel = GetComponent<HandModel>();
        leapHand = handModel.GetLeapHand ();
        if (leapHand == null) 
        {
            Debug.LogError ("No leapHand Founded");
        }
    }

    // Update is called once per frame
    void Update () 
    {
        Vector3 fwd = transform.TransformDirection (Vector3.forward);
        RaycastHit hit;
        for (int i = 0; i < HandModel.NUM_FINGERS; i++) 
        {
            FingerModel finger = handModel.fingers[i];
            if(Physics.Raycast(finger.GetTipPosition(),fwd,out hit))
            {
                float distanceToGround = hit.distance;
                Debug.Log ("hit something"+distanceToGround);
            }
            Debug.DrawRay(finger.GetTipPosition(),finger.GetRay().direction, Color.red);
        }

    }
}

Screen Shot 2015-12-09 at 14.43.40.png


これを応用して、Raycastを使ってtoggleとか、いろんなことができる。

3
3
0

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
3
3