Forum Discussion
jaumet2000
4 years agoHonored Guest
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 w...
- 4 years ago
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
WolffRuoff
4 years agoExplorer
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
- Shashwatmehrotra0073 years agoHonored Guest
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.- Fahruz3 years agoExpert Protege
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); } }- nico.3d2 years agoHonored Guest
Beautiful this worked for me with a little bit of tweaking, thank you.
Syntax for subscribing to the events in another scripteventSender = GetComponent<ObjectGrabbedEventSender>(); eventSender.onObjectGrabbed += OnObjectGrabbed; eventSender.onObjectReleased += OnObjectReleased;
- hasenav3 years agoExplorer
Hi! In my case, I just need to know if an interactor is grabbing an object. I have created a script that runs from the beginning of the game and receives the HandGrabInteractor for the right hand and another one for the left hand.
In the fixedUpdate method, I check if either of the HandGrabInteractors is in the "Grabbing" state.
- 0ctipusPrime2 years agoExplorer
For those who struggled to follow the solution like me:
1. Pick one of the prepared ITransformers that is closest to your vision (eg. OneGrabFreeTransformer, TwoGrabPlaneTransformer). They can be found in the Grabbable folder of the package Meta XR Interaction SDK, but you can just search for them in your project tab in Unity.
2. Create a copy of it into your assets (because you can't modify them in the package)
3. Modify functionality however you see fit.
For example add `GameManager.ObjectSelected(_grabbable.Transform.gameObject);` like me to track which object has been selected. The `_grabbable.Transform.gameObject` is the reference to the object that is grabbed. To make my example with you need to implement event triggering ObjectSelected method in the GameManager. BeginTransform might look like this:
public void BeginTransform()
{
Pose grabPoint = _grabbable.GrabPoints[0];
var targetTransform = _grabbable.Transform;
_grabDeltaInLocalSpace = new Pose(targetTransform.InverseTransformVector(grabPoint.position - targetTransform.position),
Quaternion.Inverse(grabPoint.rotation) * targetTransform.rotation);
GameManager.ObjectSelected(_grabbable.Transform.gameObject);
}4. Add this new ITransform script to the grabbable object
5. Find Grabbable in the inspector of the GameObject, expand the Optionals and drag the component (that is attached to the same game object from step 4) into the field One (or Two) Grab Transformer.
6. Test whether your changes in the script had an effect.
This is how the object you want to grab might look like.
- hannhudd2 years agoHonored Guest
This was so helpful-- thank you!
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 2 years ago
- 8 years agoAnonymous