Grab Example

Forums:

As you have probably seen in some of the other Unity posts is the use of the DistanceGrab example to pick up objects in your scene. You will need to install Oculus XR Plugin and the XR Interaction Toolkit (both of which are out of scope for this topic).
Unfortunately, the XR Interaction Toolkit is still beta mode so you will not find it in the standard installs. You will have to turn on the ability to allow beta packages. To me, this makes no sense since it is a valuable tool for working with the Oculus devices. Once you have these installed you can load an example scene "Distance Grabbable" which allows you to pick up objects from a distance around the scene, including examples like using crosshairs, outlines, etc. The one thing that confused me was the difference betwixed DistanceGrabbable and OVRGrabbable. What cleared this up is that the distance grabbable class inherits OVRGrabbable, which you can see in the class: public class DistanceGrabbable : OVRGrabbable To leverage distance grabbable, you need to set the grabbable object on the "grabbable" layer. The "Grabber" side script is installed on the PlayerController object which you can inspect in the scene as well. You can also create your own scene and copy/paste the player controller and some of the grabbable objects which will give you a quick start to building your own grabby scene. Just copy in one scene, switch to your scene and then paste the components. There are a few things to get grabbing to work. 1. Your playerController object will require the grab manager script provided in the example scene. It will also require the two DistanceGrabHandLeft/Right objects under the LeftControllerAnchor child object provided form the example scene. 2. Any "Grabbable" objects will require the distance grabbable script provided in the example scene. 3. Any grabbable objects will need to be in the grabbable Layer. 4. Grab offsets control where the grabbed object snaps when picked up. You can leave this empty and it will use the default, which I believe is the gripTrans transform on the playerObject. A simple scripting concept is to play audio on an object when you grab it using isGrabbed. When you declare grabbable in your code however you do not declare DistanceGrabbable you decare OVRGrabbable instead. If you serialize (in bold) grabbable on your object that has "DistanceGrabbable" script installed, you can drag that grabbable object onto the script params. (Probably not required but I like to literally drop the referenced object onto the script.) Initialize the objects on Start and then use Update to see if the object is being grabbed and act on it but turning off the audio source. After all, grabbing something is one thing but making something happen when you do is a whole 'nuther can of beans. You could also check for gameObject tags to make sure "what is grabbing" is the grabber you want to allow but I did not do that here. Sample c# Grab Interaction Script using System.Collections; using System.Collections.Generic; using UnityEngine; public class AlarmClock : MonoBehaviour { [SerializeField] AudioSource clockAudio; [SerializeField] OVRGrabbable distanceGrabbable; void Start() { clockAudio = GetComponent(); //initialize audio distanceGrabbable = GetComponent(); //initialize grab if(Challenges.CurrentScore > 0) { if (clockAudio != null) { clockAudio.Stop(); } } } void Update() { if(distanceGrabbable != null && clockAudio != null) //check if grab and audio source are both available { if (distanceGrabbable.isGrabbed)//was grabbed { clockAudio.Stop();//stop audio Challenges.IncrementScore(1); } } } }//class over As you can see, upon grab validation, the audio source is stopped and the score is incremented by 1. Challenges is a class I created to handle the scoring and levels and then saving it to the player settings. Under start I added a check to see if I should stop the Alarm clock audio from playing if the CurrentScore is > 0. My next step will be to "enable" the door open animation so you can actually leave the house and head over to the ADayInIT Office to apply for a job. This will probably be based on a few more "training" missions.