Oculus Touch Tutorial Updates

Gerald McAlister | May 24, 2017 | Developer Diary


As many of you have noticed, the Oculus Avatar and Unity SDKs have been updated since our initial posts, and our initial tutorials have broken as a result. We’re working on updating the original tutorials, as well as creating an archive section to keep them around.  You can read more about this in the recent update we posted. For now however, we want to go ahead and show how you can update your existing code based on the original tutorials, as this is helpful for understanding in more detail what our code is doing. So let’s dive on in to how we updated these tutorials to work with the new Oculus SDK.  Before you begin, make sure you’ve done the tutorials here and here to start!

Avatar SDK Updates

To start, go ahead and download the latest version of the Avatar SDK (1.14 as of this post).  Add that to your project, but don’t overwrite the existing Local and Remote Avatar prefabs.  We want to make sure that the custom components we’ve added remain.  Next, you’ll want to update the OvrAvatarSkinnedMeshRenderComponent script in the exact same way we did in our previous tutorials since this will have been overwritten.  The code from the tutorials does not need to be updated at all, meaning it should look like this in the end.

Moving on, we now need to update the OvrAvatarHand script.  You’ll notice that this script is now completely empty after this update, and we can no longer rely on the Update method that was previously used.  We will need to update it like so as a result:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class OvrAvatarHand : MonoBehaviour
{
    public OvrAvatarDriver AvatarDriver;
    public Hand handScript;
    float alpha = 1.0f;
    Rigidbody rigidBody = null;
    OVRInput.Controller controller;

    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        if (name.ToLower().Contains("left"))
        {
            controller = OVRInput.Controller.LTouch;
        }
        else
        {
            controller = OVRInput.Controller.RTouch;
        }
    }

    public void Update()
    {
        if (rigidBody != null)
        {
            rigidBody.detectCollisions = handScript.mHandState == Hand.State.EMPTY && OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, controller) >= 0.75f;
        }
    }
}

The Start of this script simply gets the Rigidbody on the component and assigns the appropriate controller based on which hand the script is attached to.  In each update, it checks if we can detect collisions based on whether the hand state is empty and if we are holding the triggers past a certain point, in the same way as our previous script.  The big difference here is that we have to manually assign a controller now, since we no longer get this from the driver like before, and perform our checks in a Unity Update method instead.

At this point, everything should work exactly as before with the latest Avatar SDK.  Let’s now move on to updating the Oculus Utilities for Unity now.

Updating Oculus Utilities

Next, we need to update the Oculus Utilities package.  This is also pretty straightforward.  Begin by downloading the latest version (1.14 as of this post) and adding it to your project.  Update everything in the package to ensure that it is the latest.  It may ask you to further update the package afterwards, go ahead and say yes, then restart your Unity editor.

When this is completed, there are two lines of code that need to be updated.  In the Hand script we created originally. update line 50 in our Update method to the following:

mOldVelocity = OVRInput.GetLocalControllerAngularVelocity(Controller);

This is because we are no longer receiving a quaternion from this method, but instead a vector3, meaning we no longer need the eulerAngles at the end of it now.  The same will also be done for line 85 in our throwObject method:

mHeldObject.angularVelocity = -OVRInput.GetLocalControllerAngularVelocity(Controller);

Notice that we also no longer need the Deg2Rad, this is because the GetLocalControllerAngularVelocity returns its vector in Radians per second already, so we no longer need to conver this.  At this point, everything should once again be functioning as expected.  As a bonus, let’s go ahead and update the platform SDK as well.

Updating the Platform SDK

This is the easiest to update of them all, in that it really requires no changes to our existing code.  Simply go ahead and download the latest version (1.14 as of this post) and import it into your project.  Nothing from this has been changed since our tutorials, so nothing should need to be done.  Once this is updated, everything should be the most recent versions and working as expected!

Conclusion

We appreciate everyone who’s commented recently to let us know that there were issues with our previous posts.  As mentioned before, we are working to update them so that this tutorial will no longer be necessary.  In the mean time, this should help get everyone updated.  We’ve also uploaded our sample to our Github, and have included these updates in there as well.  Each commit is from a different section of this tutorial as well, so you can follow along from a specific point by simply reverting back to the desired commit.  If you run into more issues, please do no hesitate to let us know in the comments below! Happy deving!