Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question XRSimpleInteractable - trigger action

Discussion in 'XR Interaction Toolkit and Input' started by vladk, Jan 18, 2021.

  1. vladk

    vladk

    Joined:
    Jul 10, 2008
    Posts:
    167
    Hello.

    I'm trying to make a XRSimpleInteractable to react on trigger instead of grip/select button. When I hook up OnSelectEntered function - it works when I press grip/select on both controllers. However when I hook up OnActivate function - then when I press trigger button it does nothing.
    Please explain where is my mistake and how do I fix it?

    Update:
    From the forum posts I figured out OnActivate is only called when the target is grabbed/selected.
    I made my own implementation of the class inherited from XRBaseInteractable to make it automatically select the object when we "OnHoverEntered" it, but still it only works only when the grab button is presset. What am I doing wrong? How do I get rid of pressing grab/select button and just use trigger?

    Code (CSharp):
    1. namespace UnityEngine.XR.Interaction.Toolkit
    2. {
    3.     [SelectionBase]
    4.     [DisallowMultipleComponent]
    5.     [AddComponentMenu("XR/XR Custom Keyboard Interactable")]
    6.     public class XRKeyboardInteractable : XRBaseInteractable
    7.     {
    8.         protected override void OnHoverEntered(XRBaseInteractor interactor)
    9.         {
    10.             base.OnHoverEntered(interactor);
    11.             onSelectEntered?.Invoke(interactor);
    12.         }
    13.  
    14.         protected override void OnHoverExited(XRBaseInteractor interactor)
    15.         {
    16.             base.OnHoverExited(interactor);
    17.             onSelectExited?.Invoke(interactor);
    18.         }
    19.     }
    20. }
     
    Last edited: Jan 18, 2021
  2. vladk

    vladk

    Joined:
    Jul 10, 2008
    Posts:
    167
    If anyone comes around this issue - I didn't found a proper way to do it. My workaround was override the XRRayInteractor class to switch actions in the ActionBasedController for the needed hand when it points at my custom interactable object like this:


    Code (CSharp):
    1. namespace UnityEngine.XR.Interaction.Toolkit
    2. {
    3.     public class XRCustomRayInteractor : XRRayInteractor
    4.     {
    5.         private ActionBasedController myActionCtrl = null;
    6.  
    7.         protected override void Start()
    8.         {
    9.             base.Start();
    10.             myActionCtrl = GetComponent<ActionBasedController>();
    11.         }
    12.  
    13.         protected override void OnHoverEntered(XRBaseInteractable interactable)
    14.         {
    15.             base.OnHoverEntered(interactable);
    16.  
    17.             if (interactable is XRKeyboardInteractable)
    18.                 SwapActions();
    19.         }
    20.  
    21.         protected override void OnHoverExited(XRBaseInteractable interactable)
    22.         {
    23.             if (interactable is XRKeyboardInteractable)
    24.                 SwapActions();
    25.  
    26.             base.OnHoverExited(interactable);
    27.         }
    28.  
    29.         private void SwapActions()
    30.         {
    31.             InputSystem.InputActionProperty tmp = myActionCtrl.selectAction;
    32.             myActionCtrl.selectAction = myActionCtrl.activateAction;
    33.             myActionCtrl.activateAction = tmp;
    34.         }
    35.     }
    36. }