- Home /
[Oculus SDK] How to add colliders to OVRGrabbable at runtime?
I generate objects at runtime that need to be grabbable. In the editor this is easy: drag the collider over the Grab Points field of OVRGrabbable and you're done. But how do I do this in script?
Actually I have this working already but only if my grabbable object has a single collider, not a compound collider. If anyone wants that, just add this code snippet to the OVRGrabbable script:
virtual public void CustomGrabCollider(Collider collider)
{
m_grabPoints = new Collider[1] { collider };
}
And then you call that wherever you generate your object and send the object's collider as argument. It's worked well for me, but now I need to do it with compound colliders.
In the editor it is again much easier. Even with compound colliders, I just drag my child object with all of the colliders attached over the Grab Points field (once the game is running and the object generated), and grab points becomes volume "1" with what looks like a single collider and it works perfectly, and I can grab the object at any point on its compound collider. Obviously now I need this to happen automatically in script and to not be reliant on me adding it from the editor at runtime. Can't figure it out though. This is the best I have so far (This is added into OVRGrabbable):
virtual public void CustomGrabColliders(Collider[] colliders)
{
m_grabPoints = colliders;
}
But it doesn't work. m_grabPoints shows a volume of 0 in the editor when I use this method.
So, how does the editor add the colliders to m_grabPoints if you just drag an object with colliders onto the protected field? Does anyone know how to make compound colliders work with OVRGrabbable at runtime?
Answer by tuf91497 · Feb 03, 2019 at 09:14 PM
Got it:
Go into the OVRGrabbable script and add this line under the m_grabpoints definition:
[SerializeField]
protected Collider[] m_grabPoints = { };
public Collider[] M_GrabPoints { get { return m_grabPoints; } set { m_grabPoints = value; } }
Then, when you have your compound (or single) collider, just set it like this:
grab = gameObject.AddComponent<OVRGrabbable>();
grab.M_GrabPoints = compoundColliderParent.GetComponentsInChildren<BoxCollider>();
Or, you know, however. Just feed it at least one collider.
2 things:
1st, there is already an accessor available called grabPoints which can be easily extended:
///
/// The contact point(s) where the object was grabbed.
///
public Collider[] grabPoints
{
get { return m_grabPoints; }
set { m_grabPoints = value; }
}
2nd, I don't know how you even got that to work - when calling AddComponent it calls Awake() which then throws an exception if the array is empty. Populating it after calling AddComponent is too late since Awake() is already called. Maybe the script used to be different in 2019? I'm just wondering how Oculus expected us to use this script in code, or maybe not?
Answer by glenneroo · Mar 30, 2021 at 10:14 PM
Here is the real solution: https://stackoverflow.com/questions/51503014/how-to-instantiate-an-oculus-grabbable-object-at-runtime-in-unity
In case that link dies, here is a copy/paste:
Read-only properties are properties that can be assigned only through the script that contains them. This means that you can only change the value of grabPoints from inside the OVRGrabbable.cs script.
The best way to do this is by adding a custom function inside the OVRGrabbable.cs file so that you can access and set the read only variables.
I use this one so I can also set the snapPosition and snapOrientation fields as well as the grabPoints.
public void Initialize(bool snapPosition, bool snapOrientation, Collider[] grabPoints)
{
m_snapPosition = snapPosition;
m_snapOrientation = snapOrientation;
m_grabPoints = grabPoints;
}
You'll be good enough only adding and calling the following one:
public void Initialize(Collider[] grabPoints)
{
m_grabPoints = grabPoints;
}
Also to note, comment out the Awake() function in OVRGrabbable.cs as well and instantiate as follows:
go.AddComponent<OVRGrabbable>().Init(collider);