Programming interaction

Introduction

In this section we will cover the basics of programming interactions from within a Unity C# script.

MiddleVR handles all aspects of your VR simulation :

- access to the state and events of all devices,

- cameras, viewports, stereoscopy.

For example, how to react when the user presses a button on a joystick ? Or a certain key on a keyboard ?

Check Appendix 2 - Class Hierarchy for an overview of the relationship between classes.

For a complete class reference, check the MiddleVR class reference :

Creating an interaction script

First create a script and attach it to an active object : Go into the Assets menu, then Create > C Sharp script.

Drag and drop the script to an active object.

Double-click on the script to edit it, and immediately add : Using MiddleVR_Unity3D;

Input devices

MiddleVR has an object that manages all the devices : the device manager.

You can query the device manager for the keyboard and mouse states:

Note: The Unity package of MiddleVR contains this sample script : MiddleVR/Scripts/Samples/VRInteractionTest.

The device manager holds the reference to all declared devices. If you want to get access to trackers' data, or the state a joystick, you will first have to ask a reference of the corresponding object to the device manager :

Note: The Unity package of MiddleVR contains this sample script : MiddleVR/Scripts/Samples/VRInteractionTest.

Accessing wand data

It is very easy to access the axis values and buttons states of the Wand through the device manager:

Note: The Unity package of MiddleVR contains this sample script : MiddleVR/Scripts/Samples/VRInteractionTest.

You can also access the Wand data from a Javascript:

Note: In order for the Javascript to correctly compile, you have to move the MiddleVR folder into the "Standard Assets", "Pro Standard Assets" or "Plugins" so that the VRManager script is compiled before the Javascript. Otherwise the Javascript will complain that VRManagerScript is an unknown type. For more information see Unity Script Compilation (Advanced) : http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

The display manager

The display manager (MiddleVR.VRDisplayMgr) is responsible for 3D nodes, cameras, viewports and display management:

Note: The Unity package of MiddleVR contains this sample script : MiddleVR/Scripts/Samples/VRInteractionTest.

Converting data from MiddleVR to Unity3D

As explained in the Unity coordinate system section, MiddleVR and Unity use a different coordinate system. When you get a 3D coordinate (3D vector, quaternion, matrix) from a tracker or a 3D node of MiddleVR, you have to convert it before using it to Unity :

Debugging with MonoDevelop

You can use MonoDevelop to build applications using MiddleVR.

You just need go in the MonoDevelop menu "Tools > Preferences > Unity > Debugger" and disable "Build project in MonoDevelop" :

Troubleshooting

What to do if things don't work as expected ? Check the "Troubleshooting - Unity3D - Programming interactions" section.