Here you can download a starting Unity package with the asset of room, just to have an environment to start.
Go to Unity, Assets > Import Package > Custom Package... and select the one you just downloaded. Then click on Import.
You will see a new folder below the Assets one. Open it, then go to Prefabs. Drag the Room prefab into the scene. This is just to facilitate this tutorial, as this prefab only contains the rendering of a room, with a table and a few boxes.
Remove the "Directional Light" from the scene, as the Room prefabs contains lights.
Go the Assets Store tab in Unity, search and Download "SteamVR Plugin".
Once downloaded, click on Import. Make sure to import all the assests (click on "all", then on "import").
If you get the "API Update Required" message, click on "I made a backup. Go Ahead!".
Once imported you will see the SteamVR folder inside the Assets folder under the Project tab in Unity.
The SteamVR/Prefab folder contains the relevat assets that will allow you to render the camera and the Vive controllers in the scene.
Select the SteamVR prefab and drag it into the scene. This plug in handles a few things while playing, related to the interface with the SteamVR player menu.
Also drag the CameraRig prefab in the scene.
Remove the Main Camera that was already present in the Scene, as it will interfere with the CameraRig prefab.
Move the CameraRig where you would like the player to start. For example behind the table. To move the CameraRig, use the Transform propertin in the Inspector, othewise click "W" and the drag the arrows on the object.
Turn on the Controllers and hit Play! You should see the room rendering from the Vive, as well as the controllers.
Understanding the Controllers Input
In this part you will try to interact with the boxes in the scene. You want the controller to have the ability to grab objects.
Create a new folder called "Scripts", and then right click in it and select Create C# Script. Call the script "ControllerGrabObject" and open it with your favorite text editor.
The "trackedObj" is the object being tracked: the Vive controller.
Add the following to the update function:
In the code above, we first set the SteamVR_Controller.Device Controller, which allows us to control the input.
Then we check if the trackpad is touched, of if the trigger is pressed or released. If so, we print a debug message.
Select both controllers in the Scene (under CameraRig), then drag and drop the script in the Inspector. In this way the script gets attached to the controllers.
Hit play and monitor the Console while you start playing with the controller.
In this part of the tutorial you will understand how to grab objects from the scene.
First of all select both controllers in the Scene, then go in the Inspector, click on "Add Component", and search for Rigidbody. In the Rigidbody properties, check the "Is Kinematic" box, and not the "Use Gravity" one. We don't want to have any gravitational force applied on them.
In orded to have object collide with each other or to understand their intersection in Unity, the objects must have a collider component attached. As you have done for the Rigidbody one, serach and add the "Box Collider" component.
Hit Play and then click on the Scene tab. Select the Controller in the hierarchy and resize the collider box so that it wraps the end circular part of the controller only. This will allow us to have a boundary around the controller, that we can use to understand if other objects are crossing that boundary or not.
Now open the same script as before. Add this two additional variables just after the trackedObj one:
Drag the script and drop it in the Inspector, with both controllers selected. You can see that one of the properties is the Laser Prefab. Drag the Laser prefab that you find in the Prefab folder, and drop it into that field.
Hit Play! You should see a red laser coming out of the controller when you click the trackpad.
Teleporting in the Scene
In this part of the tutorial you will learn how to teleport in the scene.
Open the LaserPointer script, and add the following variables at the beginning:
In general we want to teleport only to particular objects in the scene, not to all of them. Open the Room prefab in the Hierarchy, and click on the Floor prefab. In the Unity Inspector you will see a dropdown meanu called "Layer". Open it and click on "Add Layer". Write a new name for the Layer ("Can Teleport") and hit "Enter".
We want to be able to teleport to all objects that have the "Can Teleport" layer. Add this layer to the objects you want to be able to teleport to (table, bed, ...).
Then go back to the script. In the line "if (Physics.Raycast(trackedObj.transform.position...", add the "teleportMask" variable at the end, inside the "Physics.Raycast()" call. This will allow to Raycast only to objects with a specific teleportMask.