Skip to content

Instantly share code, notes, and snippets.

@gupen
Created December 13, 2017 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gupen/5bd9e161a47a3f5ab34fe8f0b591350c to your computer and use it in GitHub Desktop.
Save gupen/5bd9e161a47a3f5ab34fe8f0b591350c to your computer and use it in GitHub Desktop.
VRsamplesのVRInputをViveコントローラで使う
//============================================
//
// 使い方:Controller (right)にアタッチ
//
//============================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//VRInputを使うためにVRStandardAssets.Utils;をusingする
using VRStandardAssets.Utils;
public class InputViveController : MonoBehaviour {
SteamVR_TrackedObject trackedObj;
private bool InputTrigger = false;
VRInput vrinput;
//========================================================================
//
// 1.コントローラの入力を取得するためにSteamVR_TrackedObjectをGetComponent
// 2.VRInput.csがアタッチされているオブジェクト(ここではMainCamera)をFind
//
//========================================================================
void Start()
{
trackedObj = GetComponent<SteamVR_TrackedObject>();
vrinput = GameObject.Find("MainCamera").GetComponent<VRInput>();
}
//======================================================================================
//
// 1.コントローラの入力を取得
// 2.トリガーの状態(押してるか押してないか)を変数に格納
// 3.VRInput.csのSetInputViveRightTrigger(bool)関数を呼び出し,引数にトリガーの状態をセット
//
//======================================================================================
void Update()
{
var device = SteamVR_Controller.Input((int)trackedObj.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetPressDown Trigger");
InputTrigger = true;
vrinput.SetInputViveRightTrigger(InputTrigger);
}
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetPressUp Trigger");
InputTrigger = false;
vrinput.SetInputViveRightTrigger(InputTrigger);
}
}
}
public class VRInput : MonoBehaviour
{
//================================================================
//
// ViveControllerのTriggerの状態(押してるか押してないか)を
// bool型変数(inputTriggerRightViveController)にセット
//
//================================================================
private bool inputTriggerRightViveController = false;
public void SetInputViveRightTrigger(bool input)
{
inputTriggerRightViveController = input;
Debug.Log(inputTriggerRightViveController);
}
//...省略
void Update()
{
CheckInput();
}
//=========================================================
//
// Viveコントローラのトリガーの状態によって処理を分岐
//
//=========================================================
private void CheckInput()
{
if (inputTriggerRightViveController == true)
{
m_MouseDownPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
if (OnDown != null)
OnDown();
}
if (inputTriggerRightViveController == false)
{
if (OnUp != null)
OnUp();
}
//...省略
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment