Oculus Questのコントローラによる入力を検知する方法をまとめました。
Unity2019.2.10
はじめに
Oculus Questのコントローラによる入力を取得するには、以下のようにUpdateやFixedUpdate内でOVRInputのメソッドを呼びます。
using UnityEngine; public class Example : MonoBehaviour { private void Update() { // 左コントローラのX Buttonが押された時にtrue if (OVRInput.GetDown(OVRInput.Button.One, OVRInput.Controller.LTouch)) { // 処理 } } }
このメソッドは第一引数に入力の種類を指定し、第二引数に左右どちらのコントローラなのかを指定します。
このUpdate内に書く方法と左右の指定については共通の処理となるので、以下では説明を省略します。
また、本記事では各ボタンやスティックの呼称は下記に従います。
ボタンが押された時・スティックが倒れた時
ボタンが押下された時やスティックが倒れた時を検知するには以下のようにします。
bool値が返ってくるので、前節のようにif文で判定します。
// X(右はA) Button押下 OVRInput.GetDown(OVRInput.Button.One, OVRInput.Controller.LTouch); // Y(右はB) Button押下 OVRInput.GetDown(OVRInput.Button.Two, OVRInput.Controller.LTouch); // Menu Button押下 OVRInput.GetDown(OVRInput.Button.Start, OVRInput.Controller.LTouch); // Thumb Stick押下 OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick, OVRInput.Controller.LTouch); // Thumb Stickを上に傾倒 OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickUp, OVRInput.Controller.LTouch); // Thumb Stickを右に傾倒 OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickRight, OVRInput.Controller.LTouch); // Thumb Stickを下に傾倒 OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickDown, OVRInput.Controller.LTouch); // Thumb Stickを左に傾倒 OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickLeft, OVRInput.Controller.LTouch); // Triggerを押下 OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.LTouch); // Grip Buttonを押下 OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger, OVRInput.Controller.LTouch);
ボタン押下を解除した時・スティックが起き上がった時
ボタンを解放した時やスティックが倒れた状態から起き上がった時を検知するには以下のように書きます。
前節のGetDownをGetUpに書き換えただけです。
// X(右はA) Button解放 OVRInput.GetUp(OVRInput.Button.One, OVRInput.Controller.LTouch); // Y(右はB) Button解放 OVRInput.GetUp(OVRInput.Button.Two, OVRInput.Controller.LTouch); // Menu Button解放 OVRInput.GetUp(OVRInput.Button.Start, OVRInput.Controller.LTouch); // Thumb Stick解放 OVRInput.GetUp(OVRInput.Button.PrimaryThumbstick, OVRInput.Controller.LTouch); // Thumb Stickを上に傾倒解除 OVRInput.GetUp(OVRInput.Button.PrimaryThumbstickUp, OVRInput.Controller.LTouch); // Thumb Stickを右に傾倒解除 OVRInput.GetUp(OVRInput.Button.PrimaryThumbstickRight, OVRInput.Controller.LTouch); // Thumb Stickを下に傾倒解除 OVRInput.GetUp(OVRInput.Button.PrimaryThumbstickDown, OVRInput.Controller.LTouch); // Thumb Stickを左に傾倒解除 OVRInput.GetUp(OVRInput.Button.PrimaryThumbstickLeft, OVRInput.Controller.LTouch); // Triggerを解放 OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.LTouch); // Grip Buttonを解放 OVRInput.GetUp(OVRInput.Button.PrimaryHandTrigger, OVRInput.Controller.LTouch);
ボタンが押されている間・スティックが倒れている間
ボタンが押下されている間、スティックが倒れている間を検知したい場合は以下のように書きます。
押下中はずっとtrueが返ってきます。
// X(右はA) Button押下中 OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.LTouch); // Y(右はB) Button押下中 OVRInput.Get(OVRInput.Button.Two, OVRInput.Controller.LTouch); // Menu Button押下中 OVRInput.Get(OVRInput.Button.Start, OVRInput.Controller.LTouch); // Thumb Stick押下中 OVRInput.Get(OVRInput.Button.PrimaryThumbstick, OVRInput.Controller.LTouch); // Thumb Stickを上に傾倒中 OVRInput.Get(OVRInput.Button.PrimaryThumbstickUp, OVRInput.Controller.LTouch); // Thumb Stickを右に傾倒中 OVRInput.Get(OVRInput.Button.PrimaryThumbstickRight, OVRInput.Controller.LTouch); // Thumb Stickを下に傾倒中 OVRInput.Get(OVRInput.Button.PrimaryThumbstickDown, OVRInput.Controller.LTouch); // Thumb Stickを左に傾倒中 OVRInput.Get(OVRInput.Button.PrimaryThumbstickLeft, OVRInput.Controller.LTouch); // Triggerを押下中 OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, OVRInput.Controller.LTouch); // Grip Buttonを押下中 OVRInput.Get(OVRInput.Button.PrimaryHandTrigger, OVRInput.Controller.LTouch);
ボタンやスティックに手が近づいたとき
ThumbStickやTriggerにはセンサーが付いているため、指が近づいたことを検知できます。
これらを検知するには以下のように記述します。
// ThumbStickやその周辺のボタンに接近 OVRInput.GetDown(OVRInput.NearTouch.PrimaryThumbButtons, OVRInput.Controller.LTouch); // Triggerに接近 OVRInput.GetDown(OVRInput.NearTouch.PrimaryIndexTrigger, OVRInput.Controller.LTouch);
ボタン押下時の強さ
Grip ButtonやTriggerは押下時の強さを取得することもできます。
以下のように書くと押下した強さがfloat値で取得できます。
// Grip Buttonの押下の強さをfloatで取得 OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, OVRInput.Controller.LTouch); // Triggerの押下の強さをfloatで取得 OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.LTouch);
スティックの倒れ具合
ThumbStickは、どの方向にどれくらい倒れているかを取得できます。
以下のように書けば方向と倒れ具合がVector2の値として返ってきます。
// ThumbStickの倒れ具合をVector2で取得
OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, OVRInput.Controller.LTouch);
よりローレベルな取得方法
以上、Oculus Quest用のコントローラにおける入力の検知についてまとめました。
もしOculus Quest用のコントローラ以外にも対応したい場合にはもっとローレベルな取得の仕方もできます。
これについては本記事では触れませんが、以下のドキュメントに詳しく書いてあります。