When it comes to programming your Oculus controllers to interact with and manipulate objects, there’s no need to start from scratch. Here’s what code to include to start grabbing and releasing objects with Oculus in Unity.

In this article we’re going to breakdown the necessary code you’ll need to start grabbing and manipulating objects with Oculus. This will not only make your app more interactive, but it will also help you understand the basics behind parent-child relationships in coding. We go over all this and more in our VR course, so if this piques your interest, check out our syllabus below!

Download our 10-week VR Development with Unity Course Syllabus


 

Skip to the Technical Steps

 

Step 1: Getting Started

 
To start off, we’re going to set up your Oculus with Unity. We cover that in detail in this article here, so mosey on over there. Once you have Oculus set up with Unity, we’re ready to begin these next steps.

Navigate back to your Assets folder where you see the other folders labeled Oculus and Scenes. Right-click the grey area, and select Create > C# Script.

Double-click on your new C# script after naming it something specific, and it’ll open up in Visual Studio Code. It’ll begin with:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class OculusGrab : MonoBehaviour {

Make sure your script name is included in the above line where we have OculusGrab as a placeholder.

Next, we’ll add the next couple of lines so that Unity can identify some objects:

public GameObject CollidingObject;
public GameObject objectInHand;

 

Step 2: How to Grab Something

 
Let’s build in some trigger zones on your controllers. These tell Unity what area to monitor (your hands) when wanting to grab or release objects.

Under LocalAvatar in your hierarchy tab, click on ‘controller_left.’ In the Inspector, add a Component, scroll to Physics, and select SphereCollider. Once that’s loaded in, click on the ‘Is Trigger’ checkbox. Do the same to the ‘controller_right.’

Last but not least, click and drag your script (labeled OculusGrab in the gif) into the controllers’ Inspector so it’s added as a component. Any updates we do to the script will now be applied to the controllers.

Great! Now that we have these trigger zones set up and have applied our script to the controllers, let’s head back into Visual Studio Code and add in functions when an object enters these zones.

The following adds the function for picking up the object (as long as it has a rigidbody) when you press the trigger button:

public void OnTriggerEnter(Collider other) 
    {
    if(other.gameObject.GetComponent())
        {
            CollidingObject = other.gameObject;
        }
    }

And this releases the object when you release the trigger button:

public void OnTriggerExit(Collider other) 
    {
        CollidingObject = null;
    }

 

Step 3: Making It Smooth

 
Immersion is easier to achieve when interaction is as smooth and seamless as possible. When it comes to interacting with objects, it will become clear very quickly whether your app runs like a well-oiled machine, or like a diesel engine.

Unity is already set up to update frames so your app reacts quickly to what you do. These next lines add a couple of extra functions during the update phase:

void Update () 
    {
    if(OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) > 0.2 && CollidingObject)
        {
            GrabObject();
        }
    if(OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) < 0.2 && objectInHand)
        {
            ReleaseObject();
        }
    }

In other words, if pressure on the trigger is over 20% and there's an object in your trigger zone, you grab and continue holding that object. If that pressure drops to less than 20%, you release that object.
 

Step 4: Building the Relationship Between Controller and Object

 
This code works according to a parent-child relationship between the object and your controller. What this means is the object follows the controller's movement until it's released.

To create this relationship, use the next lines:

public void GrabObject() 
    {
        objectInHand = CollidingObject;
        objectInHand.transform.SetParent (this.transform);
        objectInHand.GetComponent().isKinematic = true;
    }

And to remove this relationship when you want to release the object, use these next few lines:

private void ReleaseObject() 
    {
        objectInHand.GetComponent().isKinematic = false;
        objectInHand.transform.SetParent (null);
        objectInHand = null;
    }
}

 

You Did It!

 
Good job! We've built in trigger zones on your controllers and have told Unity what to do when objects with rigidbodies enter those zones. Even better, now you can pick up those objects, have them follow your controller's movements, and release them with ease.

You made it one step closer to making your Oculus app more interactive. If you thought this article was helpful, check out our 10-week VR course! We'll take you from picking up objects, to juggling them.

Download VR Course Syllabus

Technical Steps:

 

[csharp]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class OculusGrab : MonoBehaviour {
 
// identifying objects
public GameObject CollidingObject;
public GameObject objectInHand;
 
// trigger functions after adding trigger zones to controllers and adding script to controllers
public void OnTriggerEnter(Collider other) //picking up objects with rigidbodies
    {
    if(other.gameObject.GetComponent())
        {
            CollidingObject = other.gameObject;
        }
    }
public void OnTriggerExit(Collider other) // releasing those objects with rigidbodies
    {
        CollidingObject = null;
    }
 
void Update () // refreshing program confirms trigger pressure and determines whether holding or releasing object
    {
    if(OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) > 0.2 && CollidingObject)
        {
            GrabObject();
        }
    if(OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger) < 0.2 && objectInHand)
        {
            ReleaseObject();
        }
    }
 
public void GrabObject() //create parentchild relationship between object and hand so object follows hand
    {
        objectInHand = CollidingObject;
        objectInHand.transform.SetParent (this.transform);
        objectInHand.GetComponent().isKinematic = true;
    }
 
private void ReleaseObject() //removing parentchild relationship so you drop the object
    {
        objectInHand.GetComponent().isKinematic = false;
        objectInHand.transform.SetParent (null);
        objectInHand = null;
    }
}
[/csharp]