Reader small image

You're reading from  Unity Virtual Reality Projects

Product typeBook
Published inSep 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781783988556
Edition1st Edition
Languages
Right arrow
Author (1)
Jonathan Linowes
Jonathan Linowes
author image
Jonathan Linowes

Jonathan Linowes is a VR/AR enthusiast, Unity, and full stack developer, entrepreneur, certified Unity instructor, and the owner of Parkerhill XR Studio, an immersive media, applications, and game developer. Jonathan has a bachelor of fine arts degree from Syracuse University, a master of science degree from the MIT Media Lab, and has held technical leadership positions at Autodesk and other companies. He has authored multiple books on VR and AR published by Packt Publishing.
Read more about Jonathan Linowes

Right arrow

Chapter 4. Gaze-based Control

Up to this point, our diorama is a third-person virtual reality experience. When you go into it, you're like an observer or a third-person camera. Sure, you can look around and add controls that let you move the camera's viewpoint. However, any action in the scene is from a third-person perspective.

In this chapter, we'll pretty much stay in the third-person mode, but we'll get a little more personally involved. We will explore the techniques that can be used to control objects in your virtual world by looking and staring. Our character, Ethan, will be under your control, responding to where you look. Furthermore, we'll start programming the Unity scripts. Along the way, we will discuss the following topics:

  • Adding AI (short for artificial intelligence) and NavMesh to our third-person character, Ethan

  • Unity programming in C#

  • Using our gaze to move a 3D cursor

  • Shooting and killing Ethan, the zombie, to good effect

Ethan, the walker


A starting point for much of VR is gaming. So, we might as well start out from there, too! We are going to give our character, Ethan, a life of his own. Well, sort of (or not), because he's going to become a zombie!

We left off at the diorama, with Ethan hanging out. You can make him run around the scene if you have a keyboard or gamepad, but in today's VR having that is not guaranteed. In fact, if you're viewing the scene with a Google Cardboard, it's pretty unlikely that you'll have a handheld controller (notwithstanding the Bluetooth game controllers). Let's be honest. Even with the Oculus Rift, requiring a keyboard or a gamepad controller is not very user-friendly, since you can't see your hands. Perhaps, there's another way to make him move around. One technique is to use the direction of your gaze while wearing your VR headset.

Before we attempt this, we'll first transform Ethan into a zombie and have him walk around aimlessly without any user control. We'll do this...

Go where I'm looking


In this next script, instead of being random, we'll send Ethan to wherever we look. In Unity, this is accomplished by using ray casting—like shooting a ray from the camera and seeing what it hits (for more information, visit http://docs.unity3d.com/Manual/CameraRays.html).

We're going to create a new script, which will be attached to WalkTarget like before, as follows:

  1. Select the WalkTarget object in the Hierarchy panel or the Scene view.

  2. In its Inspector panel, click on the Add Component button.

  3. Select New Script.

  4. Name it LookMoveTo.

  5. Ensure that the C Sharp language is selected.

  6. Click on Create and Add.

This should create a script component on the WalkTarget object. Double-click on it to open it in the MonoDevelop code editor.

The LookMoveTo script

In our script, each time Update() is called, we'll read where the camera is pointing (by using its transform position and rotation), cast a ray in that direction, and ask Unity to tell us where it hits the ground plane. Then, we'll...

If looks could kill


We got this far. We might as well try to kill Ethan (haha!). Here are the specifications for this new feature:

  • Looking at Ethan hits him with our line-of-sight ray gun

  • Sparks are emitted when the gun hits its target

  • After 3 seconds of being hit, Ethan is killed

  • When he's killed, Ethan explodes (we get a point) and then he respawns at a new location

The KillTarget script

This time, we'll attach the script to a new empty GameController object by performing the following steps:

  1. Create an empty game object and name it GameController.

  2. Attach a new C# script to it, using Add Component, named KillTarget.

  3. Open the script in MonoDevelop.

Here's the completed KillTarget.cs script:

using UnityEngine;
using System.Collections;

public class KillTarget : MonoBehaviour {
  public GameObject target;
  public ParticleSystem hitEffect;
  public GameObject killEffect;
  public float timeToSelect = 3.0f;
  public int score;

  private float countDown;

  void Start () {
    score = 0;
    countDown...

Summary


In this chapter, we explored the relationship between the VR camera and objects in the scene. We first made Ethan (the zombie) walk randomly around the scene and enabled to move by using a NavMesh, but then, we directed his wanderings using a 3D cursor on the X, Z ground plane. This cursor follows our gaze as we look around the scene in virtual reality. Lastly, we also used our gaze to shoot a ray at Ethan, causing him to lose health and eventually explode.

These look-based techniques can be used in non-VR games, but in VR, it's very common and almost essential. We'll be using them more in the later chapters of this book too.

In the next chapter, we will look at the various ways to present the user with information and input widgets in VR. User interface (UI) conventions found in desktop applications and video games may not work well in VR, because they operate in Screen Space. However, as we'll see, there are numerous other effective ways that you can use to approach UI that do make...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Virtual Reality Projects
Published in: Sep 2015Publisher: PacktISBN-13: 9781783988556
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jonathan Linowes

Jonathan Linowes is a VR/AR enthusiast, Unity, and full stack developer, entrepreneur, certified Unity instructor, and the owner of Parkerhill XR Studio, an immersive media, applications, and game developer. Jonathan has a bachelor of fine arts degree from Syracuse University, a master of science degree from the MIT Media Lab, and has held technical leadership positions at Autodesk and other companies. He has authored multiple books on VR and AR published by Packt Publishing.
Read more about Jonathan Linowes