Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question XR Toolkit. Is there a way for an object to tell the controller/interactor to drop the object

Discussion in 'AR/VR (XR) Discussion' started by Tjen, Sep 10, 2020.

  1. Tjen

    Tjen

    Joined:
    Jan 8, 2017
    Posts:
    5
    Hi guys :)

    So here is my case:

    I have a magazine that reaches a trigger zone of a gun to reload it. Upon reaching that I want to destroy/detach the magazine I had in the hand as it now lives in the gun.

    My problem is that I cannot seem to find a way to force the XRcontroller or XR DirectInteractor to drop the magazine via code.

    I am probably missing something super obvious but I have stared myself completely blind at this issue. I have quite a few more use cases where I need an object to tell the hand that it wants to be free from it :D So any help I can get will be greatly appreciated :D

    NB: The magazine object has references to its own XRGrabInteractable and the owning hands XRDirectInteractor.

    From what I have learned it seems that the XRInteractionManager calls an OnSelectExit to unassign it, but I am not allowed to call this, only the interactionManager?

    Again thanks for any input :D
     
  2. wachichi

    wachichi

    Joined:
    Dec 6, 2018
    Posts:
    1
    Hi Tjen,

    Well, hope this isn't coming too late.

    I usually use a trick for this, by disabling the Interactor component on the hand. Not sure if this is the best way but it gets the work done. I use a wait time for 0.25s, which works quite well. Here is the method;

    Step One: Create your interactable class that extends the base-interactable.

    public class YourInteractableClass: XRGrabInteractable

    Step Two: Create the drop function (Enabling and Disabling the DirectInterator)

    private IEnumerator ObjectDropForce(XRBaseInteractor interactor)
    {
    if (interactor.TryGetComponent(out XRDirectInteractor directInteractor))
    {
    directInteractor.enabled = false;
    yield return new WaitForSeconds(0.25f);
    directInteractor.enabled = true;
    }
    }

    Step Three: Call the function (NB: Once you extend the class you should have access to the selectinginteractor variable)

    StartCoroutine("ObjectDropForce", selectingInteractor);
     
    brgishy likes this.
unityunity