Intro to VR Development with Oculus Rift
Updated: Oct 6, 2019
In this tutorial, we will create our very first VR application for the Oculus Rift using Unity and the Oculus SDK. A small amount of knowledge of navigating Unity is recommended, but if you are new to Unity, the video will walk through everything you need to know.
Unity version used: 2018.2.13f
To start, make sure you have both Oculus and Unity installed. If you have not used Oculus on this computer before or are using a shared computer, launch Oculus and follow the set-up instructions. Within the Oculus app, navigate to Settings > General > Unknown Sources and set it to Allow. This will allow Unity to natively use your headset during development.
Once you're all set up, launch Unity and create a new 3D project. No packages are required.
Once your project is open, navigate to File > Build Settings > Player Settings within Unity. Scroll to XR Settings and check Virtual Reality Enabled.
Open the Unity Asset Store tab. Search "Oculus Integration". Download and import the package. Accept any pop-ups that tell you to update plugins or restart Unity.
Once these two things are done, your project is ready for VR development to target the Oculus Rift. Keep reading to learn how to set up a basic VR example project.
Example VR Project
The VR project we will be making today will give you a rough outline of the tools the Oculus SDK provides for Unity developers. You'll learn how to add a VR character to the scene and how to make a basic grippable object.
To make sure the default Unity camera does not interfere with our player's camera, start by deleting the Main Camera from the scene. Using the "Create" menu, set up a simple scene with a Plane centered at (0, 0, 0).
We are also going to make a table to put objects on. Add a Cube to the scene, and modify the X and Z scale values so that it is not as wide. Right click on the Assets tab and click Create > Material. Name this material "Wood", and click on the white square to the right of Albedo to give it a custom color. Finally, drag the material onto the cube to apply it.
To create our player, we are going to use some pre-built assets that the Oculus SDK offers. First, navigate to Oculus > VR > Prefabs in the Asset tab. Drag an OVRCameraRig prefab directly into the scene. This prefab provides a default static GameObject with a Camera that directly works with the Oculus Rift. We won't be able to move for now, so make sure to position the OVRCameraRig to be right in front of the table you made. Also, move the OVRCameraRig up slightly by changing its Y transform so the height is correct.
To add hands to our player, navigate to Oculus > Avatar > Content > Prefabs. Make sure the OVRCameraRig in the scene is expanded so you can see its children. Drag a LocalAvatar prefab from the Asset tab into the scene as a child of the TrackingSpace object. Your setup should look something like this:
The newest version of Oculus Integration requires that an App ID be set in order to use the Avatar system. However, any App ID will be fine for this simple test. Go to the Oculus > Avatars > Edit Settings at the top of Unity. Type in 0 for Oculus Rift App Id.
If you hit play, you should be able to look around and see your hands.
Before we can make grippable objects, we need to set-up gripping on our hands. Expand the TrackingSpace object and select both the LeftHandAnchor and RightHandAnchor children by holding shift. This will allow you to modify both at the same time. First, click Add Component and add a Sphere Collider. Set Radius to 0.075, and check Is Trigger. Additionally, add a Rigidbody component and uncheck Use Gravity. Finally, add an OVR Grabber component.
Select the LeftHandAnchor individually. In the hierarchy on the left, click on LeftHandAnchor and drag it into the "Grip Transform" field in the OVR Grabber component on the inspector pane. Expand the "Grab Volumes" array, and repeat by dragging LeftHandAnchor into the array. Finally, set the controller to "L Touch". Repeat this process for the RightHandAnchor, only dragging the RightHandAnchor in and setting to "R Touch".
The hands should be properly configured to grip objects. Now, we can add a grippable object to the scene. Start by adding a Sphere object and positioning it on the table you made earlier. Modify the scale to about 0.2 in all axes, so it is fairly small. It should already come with a Sphere Collider, so all we need to add is a Rigidbody component. Finally, add an OVR Grabbable component to the sphere. This should be all you need to create a simple grabbable object! Feel free to also add a custom material to it as well.
If you wanted to clone your ball several times, you could of course copy and paste the GameObject itself. But if you want to make a change or add something to one of the balls, you would have to repeat that change on all the cloned versions as well. Unity's Prefab system allows you to re-use a GameObject as an asset and have all changes be standardized.
To turn a GameObject into a prefab, drag it from the hierarchy down to the Assets folder. The name will now be in blue in the hierarchy, and you can drag your prefab from the assets folder directly into the scene to add more copies. If you make a change to a prefab, you can hit the "Apply" button on the top of the inspector to apply changes to the base prefab and all instances of it.
If you hit play, you should have a basic scene set up where you can look around and use the motion controllers to pick up your ball by squeezing. At this point, the scene is free for you to customize by adding additional 3D Objects, Lights, Sounds, or Particle effects. A great resource to get free assets that you can start using in your experience is the Asset Store tab. It's directly integrated into Unity and gives you access to a ton of free assets to play around with. Try creating a custom scene in VR with multiple grippable objects.
The next step, which we will explore in later workshops, is to add custom C# code to create more interactive objects within the scene.