Unity
VR
OculusGo

Oculus Go 開発メモ - VRInputでスワイプするとOnClickも呼ばれてしまう件

症状

タイトルどおりで、VRInputを使ってスワイプを検出しようとすると、OnSwipeだけでなくOnClickやOnDown,OnUpなども呼ばれてしまう。

原因

VRInput.csを読んでみると、 Input.GetButtonDown("Fire1") でクリックを判定しているのだが、 Input.GetButtonDown("Fire1") はOculus Goのコントローラーのタッチパッドに触れた時(クリックした時ではない)にもtrueになるようだ。

対処

VRInput.csを以下のように書き換えた。

はじめに、どのボタンのクリックを拾いたいかを設定するフィールドを用意しておく。

VRInput.cs(抜粋)
        [SerializeField] private OVRInput.Button clickButton = OVRInput.Button.PrimaryIndexTrigger;

次に、実際に入力を判定しているメソッド CheckInput を大幅に書き換える。

VRInput.cs(抜粋)
        private void CheckInput(){
            // Set the default swipe to be none.
            SwipeDirection swipe = SwipeDirection.NONE;

            if (Input.GetButtonDown("Fire1")){
                // When Fire1 is pressed record the position of the mouse.
                m_MouseDownPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            }

            // This if statement is to gather information about the mouse when the button is up.
            if (Input.GetButtonUp ("Fire1")){
                // When Fire1 is released record the position of the mouse.
                m_MouseUpPosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);

                // Detect the direction between the mouse positions when Fire1 is pressed and released.
                swipe = DetectSwipe ();
            }

            // If there was no swipe this frame from the mouse, check for a keyboard swipe.
            if (swipe == SwipeDirection.NONE){
                swipe = DetectKeyboardEmulatedSwipe();
            }

            // If there are any subscribers to OnSwipe call it passing in the detected swipe.
            if (OnSwipe != null){
                OnSwipe(swipe);
            }

            if(OVRInput.GetDown(clickButton)){
                if (OnDown != null){
                    OnDown();
                }
            }

            if(OVRInput.GetUp(clickButton)){
                if (OnUp != null){
                    OnUp();
                }

                // If the time between the last release of Fire1 and now is less
                // than the allowed double click time then it's a double click.
                if (Time.time - m_LastMouseUpTime < m_DoubleClickTime){
                    // If anything has subscribed to OnDoubleClick call it.
                    if (OnDoubleClick != null){
                        OnDoubleClick();
                    }
                }else{
                    // If it's not a double click, it's a single click.
                    // If anything has subscribed to OnClick call it.
                    if (OnClick != null){
                        OnClick();
                    }
                }

                // Record the time when Fire1 is released.
                m_LastMouseUpTime = Time.time;
            }

            // If the Cancel button is pressed and there are subscribers to OnCancel call it.
            if (Input.GetButtonDown("Cancel")){
                if (OnCancel != null){
                    OnCancel();
                }
            }

        }

以上。