Tutorial written by Amanda End and Circuit Stream.

Welcome to Section Seven, the final section of the HTC Vive Virtual Reality Programming Tutorial. In this section we're going to cover some useful tools that will help you with your prototyping.





SteamVR Tools

The Valve VR team have created several useful tools in SteamVR that you should definitely check out. If nothing else, they really help with rapid prototyping. Most of these scripts can be found in the SteamVR Folder, under Extras.





SteamVR Gaze Tracker

This is a script that is similar to the input system for raycasting from head and hands, but it’s only for tracking whether the head is looking at an object. You can put this script on an object and then write another script to reference it’s GazeOn and GazeOff events. I like my systems to all work similarly, so for our lessons I chose to build my own input system for gazing and pointing, but if it makes more sense to you to just use this script, it’s a good alternative. Gaze In Cutoff and Gaze Out Cutoff are floats that represent how close to the object you have to be to trigger the gaze in, and how far away from it you have to be to trigger the gaze out.

SteamVR Laser Pointer

There is a laser pointer component that is very useful for a variety of purposes. The teleport script, for example, is very hard to aim with. So using it in conjunction with the laser pointer makes things easier to aim. It also has OnPointerIn and OnPointerOut events that you can subscribe to, and like the GazeTracker, you could use these as an alternative to writing a raycast input system.

Active is a public field that you can use to turn on and off the laser pointer.

is a public field that you can use to turn on and off the laser pointer. Color is the color that you want your laser pointer to be.

is the color that you want your laser pointer to be. Thickness is the thickness of the laser.

is the thickness of the laser. Holder and Pointer are public fields, but they probably shouldn’t be - both of those variables are assigned during the script’s Start, so anything you assign there will be overridden.

and Pointer are public fields, but they probably shouldn’t be - both of those variables are assigned during the script’s Start, so anything you assign there will be overridden. AddRigidBody adds a rigid body to your laser (which is just a really long cube) so that you can knock physics stuff around with your laser if you want

adds a rigid body to your laser (which is just a really long cube) so that you can knock physics stuff around with your laser if you want Reference doesn’t seem to do anything so just leave it blank. (I couldn’t find anything in the code that referenced it)

Lots of fields here, so let’s go over them.





SteamVR Teleporter

SteamVR (or openvr) comes with a handy teleport script that you can attach to the left and/or right controller. The component has two fields: Teleport On Click, and Teleport Type.

Teleport On Click

Teleport on click is an easily accessed public bool that allows you to turn on and off teleportation without having to enable and disable the component itself. You can write your own script to reference the teleport component and flip this bool when you don’t want the teleporter to be functioning. The script (found at SteamVR/Extras/SteamVR_Teleporter.cs in your assets folder) uses the trigger button, and if you want to change that you’ll have to update this section of code:

void Start ()

{

var trackedController = GetComponent ();

if (trackedController == null)

{

trackedController = gameObject.AddComponent ();

} trackedController.TriggerClicked += new ClickedEventHandler(DoClick); if (teleportType == TeleportType.TeleportTypeUseTerrain)

{

// Start the player at the level of the terrain

var t = reference;

if (t != null)

t.position = new Vector3(t.position.x, Terrain.activeTerrain.SampleHeight(t.position), t.position.z);

}

}



This is also a good example of how to use the controller’s built in event system for buttons down an up, which could be used to build an interaction system in a different way, if it makes more sense to you.





Teleport Type

The teleporter has three types: Teleport Type Use Terrain, Teleport Type Use Collider, and Teleport Type Use Zero Y





1. Teleport Type Use Terrain

This teleport sends a raycast out from your controller, and if it hits terrain, will teleport you to the spot where it hit terrain. It will not teleport you if no terrain is hit.





2. Teleport Type Use Collider

Similar to Use Terrain, but will teleport you to the spot where the raycast hits a collider. Note, in my version of SteamVR, this code is broken. If you want to use it, you’ll have to update this block of code from the DoClick method from this:

else if (teleportType == TeleportType.TeleportTypeUseCollider)

{

RaycastHit hitInfo;

Physics.Raycast(ray, out hitInfo);

dist = hitInfo.distance;

}







To this:

else if (teleportType == TeleportType.TeleportTypeUseCollider)

{

RaycastHit hitInfo;

hasGroundTarget = Physics.Raycast(ray, out hitInfo);

dist = hitInfo.distance;

}



hasGroundTarget has to be set for the teleporter to work.





3. Teleport Type Use Zero Y

This setting doesn’t require anything to be in the scene, it just teleports you to wherever the raycast crosses the Y plane at zero, which is generally the “floor”.

Whichever one you use is up to your particular situation. You might also find it more useful to download a plugin for your teleporter, if you want something fancier like an arc teleporter but don’t want to write one yourself.





Quick and Dirty Reticle

If you haven’t heard the word Reticle before, don’t worry, I had to look it up too. It basically means sight, or the lines in scope. Some sort of dot or crosshair in the center of a looking device that helps you target.

Reticle

Note: theis the pink dot. Usually you’d make it a lot less conspicuous.

I found when I was writing the lesson about gaze-based things, that I was having a hard time aiming with my head. I suspect that we rotate our head to give our dominant eye a better view, because I was always sure that I had to look to the right of something to get it to “highlight”. Putting a little reticle in your view helps you aim your head a lot. If you’ve ever used a handheld VR device, chances are you’ve seen one, since they usually use a reticle as their “cursor”, or method of interaction.

Camera (eye)

Camera (head)

NewtonVR

Reticles are pretty easy to set up, really you only need to parent something to your head. Specifically, theobject that is parented to theobject. I’ve seen some people do it with a canvas, but I personally just parented a sprite. You could even do it with a 3D object if you want, just depends on the effect you’re looking for. Experiment! Anyway, all you have to do is create your object, add it as a child to the Camera (eye), and set it’s position to something like 0, 0, 0.8. You can mess with that Z value a bit to make sure that it’s something that makes sense, but be careful about getting it too close, or it’ll look like there are two of them.

Fin

It would be remiss if I didn't take a moment to plug Newton VR by Tomorrow Today Labs , a physics-driven interaction system that you can download as a plugin and use to skip just about everything we covered in this tutorial. While I think it's important to understand how to do a lot of this from scratch, sometimes you just need to get prototyping really quickly, and I learned most of what I know about interaction systems from picking through NewtonVR's scripts.

I'm sorry to say folks, but that concludes the final section of Intro to VR Development for Unity and HTC Vive. I hope you all enjoyed it, and that you can now go out and make awesome VR applications of your own!

If you're still wondering how to become a virtual reality developer, check out our Course. In this 10-week virtual reality bootcamp you'll work directly with virtual reality and augmented reality instructors who help you build your own personal Unity VR project.