Skip to main content
New Participant
April 25, 2022
Solved

Check if an object is grabbed

 Hello guys. I can't find a way to check if an object is grabbed or not,  a boolean that is true if it is grabbed and false if it is not. The attached image is the components that the object that I want that variable to have. 

I'm using Oculus sdk Integration in Unity 2020.3.10.

Any help is appreciated. Thank you

 

 

This topic has been closed for replies.
New Participant
May 4, 2022

There might be an easier solution but when I did this, I also wanted a few other features so I created my own ITransformer and put it in the Grabbable Script. Once you do this, you can just copy-paste the code in the default ITransformer you want. In an ITransformer, there is a method called BeginTransform() that is called when an object is first grabbed and EndTransform() when it is unselected. I'd recommend saving a bool somewhere else that is changed by these methods.

 

EDIT: If you want to get the gameobject inside the iTransformer script use this (assuming the script is on the gameobject): 

_grabbable.Transform.gameObject

 

16 replies

New Participant
May 3, 2022

Hey, did you find a solution? I'm trying to understand the same thing..

New Participant
May 4, 2022

There might be an easier solution but when I did this, I also wanted a few other features so I created my own ITransformer and put it in the Grabbable Script. Once you do this, you can just copy-paste the code in the default ITransformer you want. In an ITransformer, there is a method called BeginTransform() that is called when an object is first grabbed and EndTransform() when it is unselected. I'd recommend saving a bool somewhere else that is changed by these methods.

 

EDIT: If you want to get the gameobject inside the iTransformer script use this (assuming the script is on the gameobject): 

_grabbable.Transform.gameObject

 

New Participant
March 16, 2023

Hey, Can you share your iTransformer script or explain how it was functioning so that I can understand your answer a little better ? I am trying to get a boolean for grabbing interaction and need help.
Thank you.

Up and Coming
April 13, 2023

If you're using simple grabbing, you can just extend the OneGrabFreeTransformer class (and also implement the interface so that you can override the methods) and assign it to the "One Grab Transformer" property in the Grabbable.

My script, which uses events looks like this:

using UnityEngine;
using Oculus.Interaction;

public class ObjectGrabbedEventSender : OneGrabFreeTransformer, ITransformer
{
    public delegate void ObjectGrabbed(GameObject source);
    public event ObjectGrabbed onObjectGrabbed;
    public delegate void ObjectMoved(GameObject source);
    public event ObjectMoved onObjectMoved;
    public delegate void ObjectReleased(GameObject source);
    public event ObjectReleased onObjectReleased;

    public new void Initialize(IGrabbable grabbable)
    {
        base.Initialize(grabbable);
    }
    public new void BeginTransform()
    {
        base.BeginTransform();
        onObjectGrabbed?.Invoke(gameObject);
    }

    public new void UpdateTransform()
    {
        base.UpdateTransform();
        onObjectMoved?.Invoke(gameObject);
    }

    public new void EndTransform()
    {
        //Parent class does nothing with that method so no need to call it
        onObjectReleased?.Invoke(gameObject);
    }

}

 

Participant
May 30, 2022

Hi! Yo can check the state in HandGrabInteractor:

private HandGrabInteractor handGrab;
....
if (InteractorState.Select == handGrab.State) -- then a object is grabbed

 

VrNumber1Bachelor
New Participant
May 18, 2023

Hey, I would love to know where you found those states. I am in desperate need for Documentation, but have found that Meta fails to provide any, at least any that are easily accessible or findable.

Up and Coming
June 7, 2023

As you say, there doesn't seem to be any doc about those states so you have to look in the source code of InteractorState to find out all the states of that enum, which are:

Normal,
Hover,
Select,
Disabled

 

New Participant
December 3, 2023

I solved this in a different way. I needed to know which hand was grabbing the object, not just if it was grabbed. There is a property on the HandGrabInteractable called Interactors that populates with which interactors are grabbing it. So, I added tags to the left and right HandGrabInteractors in my OVRCameraRig and then added this code in a script on the same GameObject as the HandGrabInteractable. If you just need to know if it is grabbed, you probably just to need to know if this is null or not. 

public class SomeClass : MonoBehavior {
   private HandGrabInteractable _interactable;
   
   void Start()
   {
      _interactable = gameObject.GetComponent<HandGrabInteractable>();
   }

   void Update(){
      var hand = _interactable.Interactors.FirstOrDefault<HandGrabInteractor>();
      if (hand != null)
      {
          Debug.Log("Connected to hand " + hand.gameObject.tag);
          // ... do something interesting with this knowledge
      }
   }
}
   
   

 

Inspiring
May 31, 2024

This was eassy!

New Participant
June 17, 2024

The solution I used to check if a particular object is grabbed.
Is to use SelectingPointsCount in the Grabbable script, that is attached to the target object.

It goes: 

    public Grabbable _grabbable;
    private void Update()
    {
        if (_grabbable.SelectingPointsCount > 0) // is grabbing
        { //Your code } 
    }
sirtario9
New Participant
June 24, 2024

Hey! 
I had a similar problem, I just want to share my solution.

This was made with Meta SDK v65 and Unity 2022.3.12f1

How it works:

Oculus Interactions works with 4 states that both the interactors and interactables have. https://developer.oculus.com/documentation/unity/unity-isdk-interactor-interactable-lifecycle/

If you have an Object you want to grab, you can add the Grab Interaction via the new Quick-Actions feature. This will add a Child GameObject that holds all the Grab scripts Oculus needs and set up a rigid body if not already present. You can then simply get the current state of an Interactor or Interactable via the e.g.:

GrabInteractable.State

 

Using this
I then wrote a script that one can attach to the GameObject. 

The script itself is used to snap two GameObjects together. for this to happen I needed to detect if a GameObject was let go. it holds the last state and gets the current state to detect the state change. The script assumes that the GrabInteractable is attached to the Generated Child GameObject.

        //get States for detecting if Brick has been let go
        LastState = CurrentState;
        CurrentState = GetComponentInChildren<Oculus.Interaction.GrabInteractable>().State;

        //detect if grabbed brick is let go
        // Select => currently grabbed
        // Normal/Hover => not grabbed 
        if (LastState == Oculus.Interaction.InteractableState.Select &&
            (CurrentState == Oculus.Interaction.InteractableState.Normal
            || CurrentState == Oculus.Interaction.InteractableState.Hover))
        {
            DetermineSnapKind();
        }

 Scene Graph for good measure.

New Participant
December 19, 2024

Hello,
I have tried your solution and written a following script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Oculus.Interaction;

public class GrabDetection : MonoBehaviour
{
    private GrabInteractable _grabInteractable;

    // Start is called before the first frame update
    void Start()
    {
        _grabInteractable = GetComponentInChildren<GrabInteractable>();
        if (_grabInteractable == null)
        {
            Debug.LogError("GrabInteractable component not found in children.");
            return;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (_grabInteractable == null)
        {
            return;
        }

        var currentState = _grabInteractable.State;
        Debug.Log("Current state: " + currentState);

        if (currentState == InteractableState.Normal)
        {
            Debug.Log("Current state: OBJECT NOT GRABBED");
        }

        if (currentState == InteractableState.Select)
        {
            Debug.Log("Current state: OBJECT GRABBED");
        }
    }
}

I am grabbing an object but select does not get there. Console just keeps printing "Current state: normal". Do you have an idea what could I check?