vrtk

Detect controller input in VRTK


I'm kind of new to this so sorry if I'm writing in the wrong place - let me know and I'll move / delete this comment.

I'm currently having issues detecting controller input while using VRTK.

For example, when I have a collision between two objects, I want to be able to detect what buttons are being pressed on the controllers but can't seem to work out how I can do this.

Also, I have implemented the Interact Use functionality but I'm struggling to work out how to make two buttons do different actions.

For example:

once I grab an object with the simple pointer I want one button to bring the object closer and another to move it away, but I've only managed to implement one or the other.


Any suggestions? I've looked everywhere in the docs, examples and Google and can't seem to find anything. Any help would be MUCH appreciated! Pulling my hair out here!


Solution

  • You could utilise the Grabbedmethod on the InteractableObject: https://vrtoolkit.readme.io/docs/vrtk_interactableobject#section-grabbed-1

    Or you could use the ControllerGrabInteractableObject event on The InteractGrab script: https://vrtoolkit.readme.io/docs/vrtk_interactgrab#section-class-events

    Or you could have an Update routine and check the grabbed status on the controller doing GetGrabbedObject() != null (which checks to see if the controller has an object grabbed if it's null then one isn't grabbed).

    Then you can use the ControllerEvents button bools to do something on a button press. So a script with this in that sits on the controller script alias gameobject next to the interact grab script:

    void Update() {
      if (GetComponent<VRTK_InteractGrab>().GetGrabbedObject != null) {
        var controllerEvents = GetComponent<VRTK_ControllerEvents>();
        if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Trigger_Press) {
            //Do something on trigger press
        }
    
        if (controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.Grip_Press) {
            //Do something on grip press
        }
      }
    }