Search Unity

Resolved XRIT: How to programatically drop a XRGrabInteractable held by a XRDirectInteractor?

Discussion in 'XR Interaction Toolkit and Input' started by Mandark, May 12, 2021.

  1. Mandark

    Mandark

    Joined:
    Nov 18, 2012
    Posts:
    7
    I have an XRGrabInteractable held by an XRDirectInteractor.

    How can I programmatically ungrab/drop the held XRGrabInteractable?

    I don't want to press the SELECT button on my controller to release it.

    I want a XRSocketInteractor-like behavior, when I put the grabbed object inside a trigger collider, the object should be re-parented and automatically detached from the hand controller grabbing it.
     
    Last edited: May 12, 2021
  2. dpcactus

    dpcactus

    Joined:
    Jan 13, 2020
    Posts:
    53
    If you ever figure this out, let me know. I've tried a lot of things but was unable to do this.
     
  3. Mandark

    Mandark

    Joined:
    Nov 18, 2012
    Posts:
    7
    Code (CSharp):
    1.  
    2. //This will release the held XRGrabInteractable, only to re-grab it in the next frame
    3. XRBaseInteractorRef.EndManualInteraction();
    4.  
    5. //This stopped the immediate re-grab
    6. XRBaseInteractorRef.EndManualInteraction();
    7. XRBaseInteractorRef.gameObject.SetActive(false);
    8.  
    9. //I tried this also but it didn't stop the re-grab, maybe it is valid for a single frame and I had to do it continous
    10. XRBaseInteractorRef.allowSelect = false;
    11. XRBaseInteractorRef.enableInteractions = false;
    12.  
    13. //I also tried to extend XRGrabInteractable and add a property CanBeGrabbed in order to stop the re-grab, but it didn't work
    14.  
    15. class MyGrabbable : XRGrabInteractable {
    16.  
    17. public bool CanBeGrabbed
    18. {
    19.     get => _canBeGrabbed;
    20.     set => _canBeGrabbed = value;
    21. }
    22.  
    23. public override bool IsSelectableBy(XRBaseInteractor interactor)
    24. {
    25.     if (!_canBeGrabbed)
    26.     {
    27.         Debug.Log($"{gameObject} cannot be grabbed by {interactor}");
    28.     }
    29.  
    30.     return _canBeGrabbed;
    31. }
    32.  
    33. private bool _canBeGrabbed = true;
    34.  
    35.  
    36. }
    37.  
    38.  
    For completeness answer by Unity dev:
    https://forum.unity.com/threads/force-player-to-drop-xrgrabinteractable.1061039/
     
    Last edited: May 14, 2021
    dpcactus likes this.
  4. arkantose

    arkantose

    Joined:
    Dec 22, 2021
    Posts:
    8
    You can also just call
    private XRSocketInteractor mySocket;
    private IXRSelectInteractable mySelect;
    public XRGrabInteractable myGrab;
    Start()
    {
    mySelect = myGrab;
    mySocket.StartManualInteraction(mySelect);
    }

    You would set myGrab to whatever object has that componenet and that you want grabbed. This will make the socket snatch the item from the player.