Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
DeakinCUBE's avatar
DeakinCUBE
Explorer
6 years ago
Solved

How can I toggle (enable / disable) OVRGrabbable on a Unity game object in script?

I would like to toggle OVRGrabbale on a game object, subject to a boolean condition.   As an example, a virtual "screw": - when it is screwed into a hole (engaged), it SHOULD NOT be grabbable by t...
  • DeakinCUBE's avatar
    6 years ago
    I've managed to solve it...turns out it was very easy but requires changes to OVRGrabbable.cs and OVRGrabber.cs

    For anyone else who comes across this problem:

    Step 1)
    In OVRGrabbable.cs
       
        public bool allowOffhandGrab
        {
            get { return m_allowOffhandGrab; }
            set { m_allowOffhandGrab = value; }     // Add this line to allow write-access to attribute
        }


    Step 2)
    In OVRGrabber.cs:

        protected virtual void GrabBegin()
        {
            float closestMagSq = float.MaxValue;
            OVRGrabbable closestGrabbable = null;
            Collider closestGrabbableCollider = null;

            // Iterate grab candidates and find the closest grabbable candidate
            foreach (OVRGrabbable grabbable in m_grabCandidates.Keys)
            {
                //bool canGrab = !(grabbable.isGrabbed && !grabbable.allowOffhandGrab);         // was this
                bool canGrab = !grabbable.isGrabbed && grabbable.allowOffhandGrab;              // change to this

                if (!canGrab)
                {
                    continue;
                }

    Step 3)
    Now you can toggle OVRGrabbable on/off in script by referencing the OVRGrabbable script and then setting "allowOffHandGrab" to true (can grab) or false (cannot grab)

    Eg:

        public class Screw : MonoBehaviour
        {
            private OVRGrabbable _OVRGrabbable;

           private void Awake()
            {
                _OVRGrabbable = GetComponent<OVRGrabbable>();
            }

            public void ToggleGrab(bool value)
            {
                _OVRGrabbable.allowOffhandGrab = value;    // True = can grab; False = cannot grab
            }
        }

    Hope this helps anyone else who encounters this problem.
    :-)