Skip to Main Content
Go to Penn Libraries homepage   Go to Guides homepage

Unity for Oculus Rift and Hololens: Unity & Oculus Rift

This guide provides basic information about using Unity to bring your 3D models into the Oculus Rift and Hololens

Unity & the Oculus Rift

Installation

  1. Download the desktop Oculus application.
  2. Download Unity and Visual Studio 2017. VS is often already packaged with Unity, so download Unity first.
  3. Go to https://developer.oculus.com/downloads/package/oculus-utilities-for-unity-5/ and download the Oculus Utilities package.
  4. Create a Unity ID.

Overview of Unity UI


Getting Started with Oculus and Unity

  1. In the Oculus app, go to Settings > General. Make sure that “Unknown Sources” is checked, to permit running apps you will write in addition to apps from the store.
  2. Open Unity and create a new project. Make sure Template: “3D” is selected. If prompted, sign in with your Unity ID.
  3. Go to Edit > Project Settings > Player.
  4. Find the XR Settings section and check “Virtual Reality Supported” (you should see Oculus as one of the Virtual Reality SDKs).
  5. Once it finishes downloading, unzip, find the OculusUtilities.unitypackage file and double click it to import it into Unity. When the window opens in Unity, leave everything checked and click Import.
  6. In Unity, go to File > Save Scene As… and choose a name for your scene.
  7. In the Assets folder, go to Oculus > VR > Prefabs, and click and drag OVRCameraRig into the Hierarchy Window.

  1. Right click on and delete the Main Camera that was originally there.
    1. Now the project will use Oculus’ special VR camera, rather than the built-in Unity camera.

 


Navigating the Scene Window

  1. To ZOOM in/out of your scene, you can use the mouse scroll wheel. You can also hold the ALT key and click and drag with the right mouse button.
  2. To TRANSLATE your scene view, click the hand in the upper left corner toolbar. Now you can right-click and drag to move around your scene.

  1. To ROTATE your scene view, hold ALT and click and drag with the left mouse button.

Setting up the File Structure

  1. In the Unity Project Window, find and click the Assets folder. The Assets folder will contain all of our models, textures, placards, etc.
  2. If you don’t already have a “Scenes” folder within the Assets folder, right-click and select Create > Folder. Call this folder “Scenes.” Put the scene you just made in this folder (the scene you just made is in the Assets folder).
  3. Create folders called “Models,” “Textures,” and “Materials” in the Assets folder.

Importing a Model with Texture

We recommend that you start with an .fbx file rather than an .obj file, which many modelling programs can export.

  1. Go into the Models folder. Right-click and select Import New Asset…
  2. In the file dialog window that opens, find the model (.obj, .fbx, etc.) you wish to import and select it.
    1. Unity doesn’t seem to like .obj’s very much. It might take a while to import.
  3. Your model should now be in the folder. Click and drag the model from the Project Window to the Hierarchy Window. Your model will now appear in the Scene Window.
  4. Move to the Textures folder. Right click and select Import New Asset…
  5. Find the model’s texture you want (generally an image file) and import it.
  6. Go to the Materials folder. Right click and select Create > Material. I like to name materials in the format “[texture or model name]_mat” e.g. “fort-medium_mat”
  7. In the Inspector Window, you will see your Material. Go to Textures, and click and drag the texture you just imported into the gray box next to the word Albedo on the Material in the Inspector Window. This is the base color of your material.

  1. You can also play with Metallic and Smoothness values of your material (you will see changes you make to these values appear in the box on the lower right of the screen).
  2. Once your material is as desired, click and drag the material from the Project Window to the model in the Project Hierarchy that you wish to texture (note: you might need to click on the name of your model in the Hierarchy to open its children and then drag the Material onto the child). The model should now appear textured.
  3. Use CTRL+S to save your scene. It’s good to save often!

Moving/Rotating/Scaling an Object

You will likely use a combination of Option 1 and Option 2.

Option 1

  1. To move/rotate your model the desired position, click on the model (either in the Hierarchy Window or the Scene Window). In the Inspector Window, you should see a Transform component:

  1. You can play with the values of the Position, Rotation, and Scale here.

Option 2

  1. When you select your object, you probably see red, green, and blue arrows on it in the Scene Window; these correspond to the x, y, and z-axes, respectively (see upper right hand corner of the Scene Window). You can click and drag these arrows to translate your object.
  2. Switch to rotate mode by clicking this symbol in the upper left of Unity:

  1. You should now see red, green, and blue circles on your object in the Scene Window. You can click and drag these to rotate your object.
  2. Switch to scale mode by clicking this symbol:

  1. You can use the red, green, and blue boxes to scale your object.
  2. You can translate and rotate at the same time by selecting this mode:


Testing the Scene

  1. Now that we have our basic scene setup, we want to see how it would look while running our application. Click the play button in the middle of the Unity Toolbar.
  2. Put the Oculus headset on. You should be able to see your model.
  3. To stop the test, click the Play button again.

Making a Skybox from a Spherical Photo

  1. Go to your Textures folder.
  2. Right click and import your spherical photo.
  3. Go to your Materials folder.
  4. Right click and create a new material.
  5. Call it something like “skybox_MAT.”
  6. In the Shader dropdown menu in the Inspector, choose Skybox > Panoramic.

  1. Drag your texture to the gray box next to “Spherical (HDR).”
  2. Now, go to Window > Rendering > Lighting Settings.
  3. Click the small circle next to Skybox Material and select the material you just made.
  4. You should see the background of your scene now textured with the photo you selected. You can exit out of Lighting Settings.

Making your Player Character

  1. In your Project Window, go to Oculus > VR > Prefabs.
  2. Click and drag OVRPlayerController into your Hierarchy Window (you can delete OVRCamera Rig since OVRPlayerController contains a camera, too).
  3. If your scene does not have any other built-in Unity geometry or objects with Colliders, your character will just immediately fall because of gravity once you hit play.
    1. You can test by creating a cube object (go to Scene Hierarchy, right click and hit Create 3D Object > Cube) and placing it in your scene.
    2. Resize the cube so that your player can stand and move around on top of it, then move your player character to be over the cube.
  4. Select OVRPlayerController and view it in the Inspector. Turn off “Snap Rotation” and set Rotation Amount to 0.7 (this controls how fast you can rotate your character; I find larger values to be slightly nauseating).
  5. Once you click play, you should be able to use the left and right joysticks to move about your scene.

Make Your Model “Walkable”

  1. I mentioned before that your character will fall if there are no objects with Colliders underneath them. Unity uses a system of Rigidbodies and Colliders to control its physics and movement. If an object with a Collider tries to walk through another object with a Collider, it will be prevented from doing so.
  2. OVRPlayerController has a built-in Collider (capsule-shaped) and Rigidbody, so that’s already done for us.
    1. We can change the size of our Collider if we want to make our character feel bigger or smaller.
  3. On our Inspector view of our model, click Add Component, and search for Rigidbody
    1. Under the Rigidbody parameters, turn off gravity and select “Is Kinematic.”
    2. This means our model will not move at all.
  4. Our imported model does not have a Collider, however, so if we wish to interact with it (move on it, walk near it), we should add one.
    1. Our model likely has a complex shape. Unity’s basic Colliders come in cubes, spheres, and capsules. We will approximate our model’s shape using these basic Colliders.
    2. Right click our model and select Create Empty (we want our new object to be a child of our model).
    3. Click that object and view it in the Inspector.
    4. Give it a name (“Collider1”), and then select Add Component.
    5. Search for “collider” and select your first Collider shape (based on the geometry of your model).
    6. You should see a wireframe of that object.
    7. Scale, Rotate, and Translate your Collider such that it approximates a part (or all) of your model.
    8. If you need more Colliders to approximate a complex shape, follow the same process as above.
  5. Once we’ve added our Colliders, our player character should be able to walk on top of and not move through our object.

Giving Your Player “Hands”

For the now the hands will be cubes, but you can do this with any mesh you import.

  1. Expand the OVRPlayerController object within the Scene Hierarchy, down to LeftHandAnchor and RightHandAnchor.
  2. Right click on LeftHandAnchor, select 3D Object > Cube. We want this cube to be a child of the LeftHandAnchor, so that when it moves, the cube also moves. Feel free to name this object something more intuitive like “LeftHand.”
  3. Do the same thing for RightHandAnchor.
  4. When you run the scene, you should be able to see your hands represented as cubes.

Making a UI Pointer

This will help us make a pointer extending from our VR hands that we can use to interact with UI.

  1. Download MinimalInputExample.rar from https://forums.oculusvr.com/developer/discussion/54513/use-gearvrcontroller-with-selection-ray-unity. You will have to scroll down or use ctrl+f to find it. This is an example project with a working pointer.
  2. No need to open the project in Unity. We just want to copy one script from it. Within the MinimalInputExample folder, open Assets/InputSystem/RayPointer.cs in Visual Studio.
  3. Now, head back to Unity and your own project.
  4. Find the EventSystem GameObject with the project hierarchy. Delete the original input module component. Click “Add Component” and search for “OVR Input Module” and add it.
  5. View your OVRCameraRig (child of OVRPlayerController) in the Inspector.
  6. Click “Add Component” and search for and add “Line Renderer.”
  7. In your Scripts folder, make a new C# script called “RayPointer.”
  8. Open the script in Visual Studio and paste in the code from the RayPointer file we got from the example project.
  9. Back in Unity, view the OVRCameraRig again. Click “Add Component” and search for and add “Ray Pointer.”
  10. Within the Ray Pointer component are settings we need to adjust:

    1. Find the “TrackingSpace” GameObject (under OVRCameraRig in the hierarchy) and drag it into the “Tracking Space” input within Ray Pointer.
    2. Find the “OVRCameraRig” GameObject and drag it into the “Line Renderer” input within Ray Pointer.
    3. Find the “EventSystem” GameObject and drag it into the “Input Module” input within Ray Pointer.
  11. At the bottom of the OVRCameraRig’s Inspector view will be a material. This represents the material of the pointer. You can change this to whatever material you want.
  12. When you make any UI object, it will be a child of a Canvas object. To make that UI interactable with the pointer, view the Canvas object in the Inspector. Remove the Graphic Raycaster component, and search and add “OVR Raycaster.”
  13. When you run the app in VR and pull the trigger of one of the Oculus Touch controllers, you should see a line extending from your hands. You should be able to use this pointer to interact with UI objects.
    1. If the pointer line is too thick or too thin, view OVRCameraRig in the Inspector. Find the Line Renderer component and find the “Width” input. Try a value like 0.01.
    2. If your line is too short or too long, view OVRCameraRig in the Inspector. Find the Ray Pointer component and play with the “Ray Length” input. Try a value like 500.

Making an object toggle-able

  1. Create an object with the Scene Hierarchy that contains some sort of MeshRenderer; this can be a cube, sphere, or some other model you import. In the scene, move this object to somewhere visible by the player character.
  2. Within your Assets folder, right click and select Create > Folder. Call it “Scripts.”
  3. Go into the Scripts folder. Right click and select Create > C# Script. Name it something like “Toggleable.”
  4. Double click on the script, and it should open in Visual Studio.
  5. Copy this into the script after the class declaration:
private MeshRenderer meshRenderer; 

// Use this for initialization void Start () {
        meshRenderer = GetComponent<MeshRenderer>(); 
} 

// Update is called once per frame void Update () {
        if (OVRInput.GetDown(OVRInput.Button.One))
        {
            meshRenderer.enabled = !meshRenderer.enabled;
        }
 }
  1. What this does is get access to the MeshRenderer component of whatever object it is attached to, in Start(), which is run once at the start of the scene. Then in Update(), which is called every frame, it checks if the a-button of the Oculus Touch has been pressed. If it has, it toggles MeshRenderer on or off, depending on its last state.
  2. Click on the object you wish to make toggleable in the Scene Hierarchy and view it in the Inspector.
  3. Click “Add Component” and search for the name of your script, adding it to the object.
  4. When you run the scene, you should be able to press a to turn the object’s visibility on and off.

Adding your project to GitHub

  1. Go to Edit > Project Settings > Editor. In the Inspector, look for “Version Control” and change its mode to “Visible Meta Files.”
  2. Look for “Asset Serialization” and change its mode to “Force Text.” This controls how Assets like scenes and prefabs are written out in their files. The “Force Text” option makes them more human readable and easier to diff (otherwise files will often contain binary).
  3. Save your project and exit Unity.
  4. In your project directory, create a .gitignore with the following:
# Unity gitignore start

Library/
ProjectSettings/
Temp/

# Auto-generated VS/MD solution and project files
*.csproj
*.unityproj
*.sln

# Unity gitignore end

The Library and ProjectSettings are generated locally and get modified practically every time Unity is opened, so we want source control to ignore these changes.

  1. You can now create a repository for your Unity project as normal.
  2. Because models and other game asset files can be very large, you might run into issues with file size. If a file is too large for git, check out Git Large File Storage (LFS): https://help.github.com/articles/configuring-git-large-file-storage/.

Resources

Penn Libraries Home Franklin Home
(215) 898-7555