warrenEBB
4/3/2019 - 6:24 PM

VR - change dominant Rift controller (for UI clicking)

Oculus Integration has an OVRInputModule you put on the canvas's "Event System" - but it only lets you choose one controller as the dominant UI raycaster. So this script changes which controller is in that slot. When you pull the trigger on a controller, it becomes the dominant controller for interacting with UI. (not ideal. Sometimes people have to "click" things twice to get what they want, because first click is just making that controller dominant. maybe someone has better hack for this?)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

/*
this script goes on EACH hand anchor in oculus rift's cameraRig prefab.
then you checkmark isLeft bool in the editor for the leftHandANchor (this controls which half of the code below is used).

It checks every frame to see if the current control has had it's trigger pulled more than half way. 
If so, it goes into the EventSystem and changes the "ray transform" to be this controller.
*/

public class toggleLaserPointer : MonoBehaviour {

    public GameObject tabletControl; // EventSystem for UI. has the pointer we need to change out
    public GameObject laser; //a dummy object with no collider. i just turn this on and off so you can see line from VR hands.
    public bool isLeft = false; //flag for which hand is dominant. set in editor. determines which half of the code below is used.
	
	// Update is called once per frame
	void Update () {
        if (isLeft)
        {
            if (OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger) > 0.5)
            {
                Debug.Log("pulled trigger");
                tabletControl.GetComponent<OVRInputModule>().rayTransform = transform;
                laser.SetActive(true);
            }
            else
            {
                laser.SetActive(false);
            }
        }
        else {
            if (OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) > 0.5)
            {
                Debug.Log("pulled trigger");
                tabletControl.GetComponent<OVRInputModule>().rayTransform = transform;
                laser.SetActive(true);
            }
            else
            {
                laser.SetActive(false);
            }
        }

    }
}