質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Oculus Quest

Oculus Questは、Facebookの小会社であるOculus VRが開発したVRヘッドセットです。独立型でPCやスマートフォンは不要。6DoFのトラッキングに優れている点が特徴です。すでに販売終了となっていますが、2020年10月には後継のOculus Quest 2が販売されています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1642閲覧

OculusQuest2で移動時にHMDの向きを考慮して移動したい

netta

総合スコア43

Oculus Quest

Oculus Questは、Facebookの小会社であるOculus VRが開発したVRヘッドセットです。独立型でPCやスマートフォンは不要。6DoFのトラッキングに優れている点が特徴です。すでに販売終了となっていますが、2020年10月には後継のOculus Quest 2が販売されています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2022/05/09 01:08

前提

oculusquest2でコントローラーを使用して3D空間を移動しています。
HMDを左右に向いたりした際に向いている方向を正面にして3D空間を移動したいのですが、その方法がわからず難儀しています。

実現したいこと

HMDを左右に向いたりした際に向いている方向を正面にして3D空間を移動したい

該当のソースコード

void Update() { touch_Up = 0; // 入力:上 touch_Down = 0; // 入力:下 touch_VUp = 0; // 入力:垂直上 touch_VDown = 0; // 入力:垂直下 touch_Left = false; // 入力:左 press_Left = false; // 入力:左 touch_Right = false; // 入力:右 press_Right = false; // 入力:右 press_Back = false; // 入力:Back press_Orbit = false; // 入力:周回モード press_Bikou = false; // 入力:備考登録 press_Mesure = false; // 入力:距離測定 Vector2 touch_r = new Vector2(0.0f,0.0f); Vector2 touch_l = new Vector2(0.0f, 0.0f); string inputText = ""; // タッチパッドのタッチを認識 if(OVRInput.GetDown(OVRInput.RawButton.RThumbstick, dominantHandController)) { // 利き手のコントローラーのボタン(AまたはX)が押された時の処理 touch_r = OVRInput.Get(OVRInput.RawAxis2D.RThumbstick); //タッチ座標取得 } if(OVRInput.GetDown(OVRInput.RawButton.LThumbstick, otherHandController)) { // 利き手ではないコントローラーのボタン(AまたはX)が押された時の処理 touch_l = OVRInput.Get(OVRInput.RawAxis2D.LThumbstick); //タッチ座標取得 } // 周回モードオンオフ if(OVRInput.GetDown(OVRInput.Button.One, dominantHandController) || OVRInput.GetDown(OVRInput.Button.One, otherHandController)) { press_Orbit = true; } // 備考登録モードオンオフ //右人差し指のトリガーを押した if(OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger) && (Bikou.activeSelf == false)) { press_Bikou = true; DebugLog.Output(this.name, "press_Bikou = " + press_Bikou); } // 距離計測モードオンオフ //右中指のトリガーを押した if(OVRInput.GetDown(OVRInput.RawButton.RHandTrigger)) { press_Mesure = true; //DebugLog.savelog(this.name, "press_Mesure = " + press_Mesure); } if((touch_l.x >= 0.5 || (OVRInput.Get(OVRInput.RawButton.LThumbstickRight))) && (-0.5 <= touch_l.y && touch_l.y <= 0.5)) //右方向 { // タッチのみ touch_Right = true; } else if((touch_l.x <= -0.5 || (OVRInput.Get(OVRInput.RawButton.LThumbstickLeft))) && (-0.5 <= touch_l.y && touch_l.y <= 0.5)) //左方向 { // タッチパッドの左側がタッチされていることを認識(または左方向へスライド) // タッチのみ touch_Left = true; } else if((touch_l.y >= 0.5 || (OVRInput.Get(OVRInput.RawButton.LThumbstickUp))) && (-0.5 <= touch_l.x && touch_l.x <= 0.5)) //上方向 { // タッチパッドの上側がタッチされていることを認識(または上方向へスライド) touch_Up++; // 上端をタッチするほど移動速度が上がる if(touch_l.y >= 0.7) touch_Up++; if(touch_l.y >= 0.9) touch_Up++; } else if((touch_l.y <= -0.5 || (OVRInput.Get(OVRInput.RawButton.LThumbstickDown))) && (-0.5 <= touch_l.x && touch_l.x <= 0.5)) //下方向 { // タッチパッドの下側がタッチされていることを認識(または下方向へスライド) touch_Down++; // 下端をタッチするほど移動速度が上がる if(touch_l.y <= -0.7) touch_Down++; if(touch_l.y <= -0.9) touch_Down++; } else if((touch_r.y >= 0.5 || (OVRInput.Get(OVRInput.RawButton.RThumbstickUp))) && (-0.5 <= touch_r.x && touch_r.x <= 0.5)) //垂直上方向 { // タッチパッドの上側がタッチされていることを認識(または上方向へスライド) touch_VUp++; // 上端をタッチするほど移動速度が上がる if(touch_r.y >= 0.7) touch_VUp++; if(touch_r.y >= 0.9) touch_VUp++; } else if((touch_r.y <= -0.5 || (OVRInput.Get(OVRInput.RawButton.RThumbstickDown))) && (-0.5 <= touch_r.x && touch_r.x <= 0.5)) //垂直下方向 { // タッチパッドの下側がタッチされていることを認識(または下方向へスライド) touch_VDown++; // 下端をタッチするほど移動速度が上がる if(touch_r.y <= -0.7) touch_VDown++; if(touch_r.y <= -0.9) touch_VDown++; } } // 1フレームごとに繰り返し、1秒間に一定回数呼び出すイベント void FixedUpdate() { //位置座標を取得 //HMDPosition = InputTracking.GetLocalPosition(XRNode.CenterEye); RectTransform LogCanvas = Log.GetComponent<RectTransform>(); // Z移動速度初期化 float z = 0.0f; // Y移動速度初期化 float y = 0.0f; if(BikouRect != null) { DebugLog.Output(this.name, BikouRect.name); } // タッチパッドの認識の仕方によって分岐 if (!arcTeleporter.moving && !menuMode) { if(press_Orbit) { if(arcTeleporter.rotateAroundMode) { // 周回モード transform.RotateAround(arcVisualizer.contactIndicator.position, new Vector3(0.0f, -1.0f, 0.0f), 45 * Time.deltaTime); // 回転軸を軸にしてカメラを右方向に周回 } } if(touch_Right) // 右方向 { // タッチのみ transform.Rotate(new Vector3(0.0f, 1.0f, 0.0f), 45 * Time.deltaTime); // カメラを軸にして右方向に周回 } else if(touch_Left) // 左方向 { // タッチのみ transform.Rotate(new Vector3(0.0f, -1.0f, 0.0f), 45 * Time.deltaTime); // カメラを軸にして左方向に周回 } else if(touch_Up >= 1) // 上方向 { if(!arcTeleporter.rotateAroundMode) { // タッチパッドの上側がタッチされていることを認識(または上方向へスライド) z += 0.5f; // 奥に平行移動 // 上端をタッチするほど移動速度が上がる if(touch_Up >= 2) z += 0.3f; if(touch_Up >= 3) z += 0.2f; } } else if(touch_Down >= 1) // 下方向 { if(!arcTeleporter.rotateAroundMode) { // タッチパッドの下側がタッチされていることを認識(または下方向へスライド) z -= 0.5f; // 手前に平行移動 // 下端をタッチするほど移動速度が上がる if(touch_Down >= 2) z -= 0.3f; if(touch_Down >= 3) z -= 0.2f; } } else if(touch_VUp >= 1) //垂直移動上 { if(!arcTeleporter.rotateAroundMode) { // タッチパッドの上側がタッチされていることを認識(または上方向へスライド) y += 0.5f; // 奥に平行移動 // 上端をタッチするほど移動速度が上がる if(touch_VUp >= 2) y += 0.3f; if(touch_VUp >= 3) y += 0.2f; } } else if(touch_VDown >= 1) //垂直移動下 { if(!arcTeleporter.rotateAroundMode) { // タッチパッドの下側がタッチされていることを認識(または下方向へスライド) y -= 0.5f; // 手前に平行移動 // 下端をタッチするほど移動速度が上がる if(touch_VDown >= 2) y -= 0.3f; if(touch_VDown >= 3) y -= 0.2f; } } // Z座標が動く場合 if (z != 0) { // 角度をカメラの向きに合わせる。 //Vector3 menuAngle = Camera.main.transform.eulerAngles; //transform.eulerAngles = new Vector3(menuAngle.x, menuAngle.y, 0); //HMDのY軸の角度取得 //Vector3 changeRotation = new Vector3(0, InputTracking.GetLocalRotation(XRNode.Head).eulerAngles.y, 0); //OVRCameraRigの位置変更 //this.transform.position += this.transform.rotation * (Quaternion.Euler(changeRotation) * changePosition); //HMDのY軸の角度取得 //Vector3 changeRotation = new Vector3(0, InputTracking.GetLocalRotation(XRNode.Head).eulerAngles.y, 0); // カメラの向き、移動速度による移動先の設定 Vector3 cameraForward = Vector3.Scale(transform.forward, new Vector3(z, 0, z)).normalized; //Vector3 PosChange = new Vector3(transform.position.x + cameraForward.x, // transform.position.y, // transform.position.z + cameraForward.z); ↓前後に移動はしてくれるものの、HMDの向きを考慮して前後に移動されない // カメラの向きに合わせて正面を移動(前進、後退)する transform.position = new Vector3(transform.position.x + cameraForward.x, transform.position.y, transform.position.z + cameraForward.z); ↑前後に移動はしてくれるものの、HMDの向きを考慮して前後に移動されない //transform.position += this.transform.rotation * (Quaternion.Euler(changeRotation) * PosChange); //transform.localPosition = transform.localPosition + Camera.main.transform.forward * Time.deltaTime; DebugLog.Output(this.name, "transform.position.x: " + transform.position.x + "transform.position.y: " + transform.position.y + "transform.position.z: " + transform.position.z); //LogCanvas.localPosition += transform.position; //DebugLog.Output(this.name, "x: " + LogCanvas.localPosition.x + "y: " + LogCanvas.localPosition.y + "z: " + LogCanvas.localPosition.z); //LogCanvas.localPosition = new Vector3(z + LogCanvas.localPosition.x, LogCanvas.localPosition.y, z + LogCanvas.localPosition.z); //LogCanvas.localPosition = new Vector3(5.0f, 5.0f, 4.0f); DebugLog.Output(this.name, "x: " + (LogCanvas.localPosition.x + transform.position.x) + "y: " + (LogCanvas.localPosition.y + transform.position.y) + "z: " + (LogCanvas.localPosition.z + transform.position.z)); LogCanvas.localPosition = new Vector3(LogCanvas.localPosition.x + cameraForward.x, LogCanvas.localPosition.y, LogCanvas.localPosition.z + cameraForward.z); } if (y != 0) { // カメラの向きに合わせて正面を移動(前進、後退)する transform.position = new Vector3(transform.position.x, transform.position.y + y, transform.position.z); } } }

試したこと

下記のサイトをためしたのですが、画面がちらついてしまいました...
https://milkyway8008.hatenablog.com/entry/2020/01/31/014244

補足情報(FW/ツールのバージョンなど)

・OculusQuest2
・Unity 2020.3.16f1
・OculusIntegrationインストール済

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。

// カメラの向き、移動速度による移動先の設定
Vector3 cameraForward = Vector3.Scale(transform.forward, new Vector3(z, 0, z)).normalized;

transform.forwardをcamera.main.transform.forwardに変えることで意図した動きになりました。

投稿2022/05/09 23:51

netta

総合スコア43

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問