LoginSignup
3

More than 5 years have passed since last update.

posted at

updated at

FirstVRでロケットパンチしてみた

さっそくFirstVRに慣れていこうと思いまして手とオブジェクトの連携をかねてロケットパンチをやることにしました。

作り方

まずSDKのシーンFVR/Samples/Scenesの中のOutputViewerを開きます。
そしてHierarchyのMuscleSensorViewerとDebug Toolsを消します。

こんな感じの見た目になるはず。

スクリーンショット 2018-04-26 16.32.54.png

そしてゾンビと手を入れてこんな感じ

スクリーンショット 2018-04-26 17.17.30.png

次にSceneManagerのSampleViewManagerを開きます。上記の青枠をクリックしてください。

変数として
public GameObject cube;

こんな感じのが宣言されているのでとりあえず
public GameObject Hand;
とかにして

Start(){
    Hand = GameObject.Find(“Hand_right”);
}

とします。”Hand_right”の部分は適宜手に連動させたいオブジェクトの名前を入れてください。

次に適当にC#ファイルを作ります。ここではhandControlとします。

開いたらまずusing FVRlib;を書き、
FVRConnnection の変数を宣言しHierarchyにあるFVRオブジェクトのFVRConnectionコンポーネントを持ってきます。

以下の感じ、

FVRConnection fvr;
fvr = GameObject.Find("FVR").GetComponent<FVRConnection> ();


次に一定以上の加速度があると腕が飛び出していくようにします。

なんとそんな加速度が閾値以上かどうかの判定を行うときに使えるfvr.accel.magnitudeっていう変数があるんです。
ちなみに飛ばすときはVector3型の変数をバッファとして用意してあげる必要があります。

以下のコードをFixedUpdateの中に入れます。

コードはこんな感じ

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using FVRlib;



public class handControl : MonoBehaviour {

  
    FVRConnection fvr;
   
    GameObject hand;
 
    Vector3 buffPosition;

 
    // Use this for initialization
   
    void Start () {
      
        fvr = GameObject.Find("FVR").GetComponent<FVRConnection>();

        hand = GameObject.Find ("Hand_right");
   
    }

 
    void FixedUpdate(){
      
        if fvr.accel.magnitude > 1.0f || Input.GetKey("z")) {

            buffPosition = hand.transform.position;
  
            buffPosition.z += 1.0f;
          
            hand.transform.position = buffPosition;
      
        }
    
    }

 
}

これを動かしたいオブジェクト(今回はHand_right)にアタッチしてスマホで実行してみます。

こんな感じ

次回は加速度を利用してものを投げるのをやってみます。

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
What you can do with signing up
3