003 - Object Interaction
In this tutorial we will look at how to interact with objects in your Oculus Rift. We will work on coloring objects when the player looks at them. Future tutorials will teach you how to actually use the items that you are registering.
1. Download the VRSamples
The first thing that we need to do is download the VRSamples package from the Unity Asset Store. This is a free package which will give us access to a lot of scripts that we will need for this tutorial series.
1.1 First open up the asset store by going to window > asset store
1.2 Use the search bar to search for "VR Samples". VR Samples should look similar to the picture below.
1.3 Download the VR Samples package and import it into your scene. It may take some time as it is a rather large package.
1.4 Close the asset store when you are done. No need to clutter your workspace.
2. Setting up your scene
The next thing we need to do is set-up our scene for VR.
2.1 I highly recommend putting in a floor for your character. To do so, just add a plane. If you are feeling adventurous, put a snazzy material on your plane. Make sure it looks good though or you might get a little dizzy. I would also move your main camera up to a y position of 8 so it doesn't look like you are a tiny child using VR.
2.2 As a check, thus far you should have a main camera, directional light, and whatever other crap you decided to put in your scene. Ok it's not crap. Stuff. Other stuff in your scene. Just like look at the picture below or something.
3. Attach Scripts to Main Camera
The next thing that we want to do is attach some scripts to the main camera. It might not be clear what we are accomplishing just quite yet, but just know that these scripts are necessary in order for us to look around and interact with objects in VR. You can find all of the script at Add Component > Scripts > VRStandarAssets.Utils > <script name>
3.1 The first script that we are going to attach is the VR Eye Raycaster script. This script will handle the raycasting from the camera as well as any Reticle GUI that is attached to the camera. At this point you can add the main camera to the Camera field. So far it should look like what we have below (we will add the reticle and vr input in a second).
3.2 Now we will add the VR Input script. This script is just taking in VR input. You don't need to change anything with this script. If you're curious, double click time affects how long you have between double clicks and swipe width affects how far you have to swipe for a swipe.
3.3 Lasts we will attach the Reticle script. The Reticle script controls the Reticle that we put on the screen. I highly recommend putting reticles of some sort onto where your character is looking to give them a good idea of where they are looking. We will fill in all of the stuff as we go, but for now you can select the Main Camera for the Camera field.
3.4 One more thing. Remember those two slots in the VR Eye Raycaster script that we didn't fill in? Now would be a good time to select the main camera and drag it into the fields. Otherwise this whole thing won't work. Should look like the image below after.
4. Adding the Reticle GUI
Ok so far it seems like we haven't accomplished anything, but we are getting closer. The next thing we need to do is add the Reticle GUI to the main camera.
4.1 Before we start adding stuff, I recommend importing some kind of image for your Reticle. When you import your image make sure that it has the traits listed below. There may be some fields that you can change such as pixels per unit, but for this tutorial just stick to the settings before.
4.2 Ok now we are ready. Let's add a canvas to our main camera. You can do so by right clicking on your main camera then going to UI and then Canvas. I renamed my canvas to VRGUI, but you can name it whatever you would like. There are a lot of settings that we need to change. Start off by changing the layer at the top to UI.
4.3 Change the render mode to World Space. This is actually how pretty much all of your UI elements are going to look. The reason is that it can cause some eye strain if you have the UI always be rendered in the camera and following you. Try it out and see for yourself though.
4.4 Change the sorting layer to GUI. This is just useful when layering different objects on top of each other.
4.5 Change the Additional Shader Channels and add TextCoord1, Normal, and Tangent. The Additional Shader Channels field will now say "Mixed...". This is going to affect how the gui is rendered in your scene.
4.6 Change the Dynamic Pixels Per Unit to 15. This is just affecting the resolution of your canvas elements. Because VR is rendered so much closer to you than normal games it is important that you are getting a good amount of resolution to your images and text.
4.7 Remove the Graphics Raycaster Component from your canvas. We do not need it for this tutorial and what we are trying to accomplish. The final canvas should look like the picture below.
5. Adding the Reticle Image
Almost done! The next thing we need to do is add the reticle image to the canvas that we just created.
5.1 Add the image to the canvas by right clicking it and then go to UI and finally click on Image. Rename the image to something similar to GUIReticle. Drag the sprite that we imported earlier onto the Source Image field.
5.2 In the material field select GUIOverlay from the list of materials. This is going to help our Reticle correctly render in the scene. This is all we need to change for the Image. You may be wondering at this point... where is my image!?
5.3 The final step is to move and size the canvas to where we can see our image. This is because it is rendered into the world space and will function just like a normal object. The image below shows what I ended up with for my final VRGUI. Yours may be different though, and that's ok.
5.4 There is one more step that we have to take. That's assigning the reticle information that we just created to our Reticle script on our main camera. It should look like what we have below once everything is set. Test it by looking around in your scene. Your Reticle should now follow you around in the game. Make slight adjustments to positioning as needed.
In this tutorial we learned how to create a GUI for our main camera. This will be very important for the next tutorial interacting with objects in the scene.