Products for USB Sensing and Control
Products for USB Sensing and Control

Interactive Maze: Spatial Phidget and Unity Software

Create an interactive Unity game with the Phidget Spatial

by Ben

Project Introduction


This project assumes you have completed the following guides:

If you've completed everything above, keep reading. If not, make sure you double back first!

Parts Inventory

HUB0000 -
VINT Hub Phidget
MOT1101 -
Spatial Phidget
Phidget Cable USB Cable


Connect your Phidget Spatial to your VINT Hub, and your VINT Hub to your computer.

Open the Phidget Control Panel and verify everything is connected properly.

Project Overview

The goal of the game is to get the ball through the maze and to the goal.

Solving the Maze

The orientation of the maze is directly linked to the Spatial Phidget.

Software Overview


After completing the Roll-a-Ball Tutorial, you'll have a play area with raised edges and yellow collectibles. You can reuse these items to create a 3D maze.

Software Flow


When your program starts, you need to create your Accelerometer object and get everything set up as usual.

For now, skip the maze creation and just have your ball on a flat surface. Generating a maze will be discussed below.


Next, you can add your events. Here's the relevant code you will need:

Attach Event

void OnAttach(object sender, AttachEventArgs e)
    //TODO: Set data interval to minimum

Detach Event

void OnDetach(object sender, DetachEventArgs e)
    //TODO: Pause game 

Acceleration Change Event

void OnSpatialData(object sender, SpatialSpatialDataEventArgs e)
    //example of accessing acceleration data!
    double xAxis = e.Acceleration[0];
    double yAxis = e.Acceleration[1];
    double zAxis = e.Acceleration[2];
    //TODO: Calculate tilt angles
    //store angles in variables for use in Fixed Update



Next, you can take a look at your update code. There are two used in this project:

  • Update
  • Fixed Update

Your Update code will handle things like an escape key being pressed to exit the game, or the space key being pressed to reset the ball and generate a new maze.

In your Fixed Update code, you should convert your angles and rotate your maze. This process is described in more detail below!

Reaching Goal

When the ball reaches the goal, you will need to end the game and print a win statement. For bonus points, you could try having multiple levels of increasing maze difficulty. When the goal is reached, the level is passed and the next stage is generated.

Closing Applications

As always, you will want to properly close your Phidget objects when an application ends. Add the following Unity method to your code:

void OnApplicationQuit()
    //TODO: Close Phidget object

Understanding Tilt


In order to implement some of the code described above, you will need to learn about angle calculations. Check out the Calculating Tilt Angles with the Spatial Phidget document.

If you've already viewed the document, here are the relevant formulas:

`theta = tan^-1 (M_y/M_z) " " phi = tan^-1 (M_x/M_z)`

`theta = "roll angle "phi = "pitch angle"`
`M_x = "Measured x-axis acceleration"`
`M_y = "Measured y-axis acceleration"`
`M_z = "Measured z-axis acceleration"`

Mapping Tilt to the Maze

Now that you've calcluated your tilt angles, the next step is converting them into a form Unity understands. Unity uses a system to describe rotations called Quaternions. You can convert your accelerometer angles to a Quaternion like so:

void FixedUpdate()
        //Convert accelerometer angles to quaternion format
        Quaternion target = Quaternion.Euler(/*pitch angle here (in DEGREES)*/, 0, /*roll angle here (in DEGREES)*/);
        //TODO: Rotate table to match accelerometer      

Hint: Check out the Slerp function on the Unity API if you are stuck.

Finishing Your Game

After making sure the maze tilts properly, the game is almost complete. Any good maze needs walls to complete it and here are two ways to go about it.

By using an automated system, users can reset their ball and generate new mazes by simply pressing the space bar.

Three randomly generated mazes.


To put the final touches on the game, try tinkering with the physics options. Changing gravity and the angular drag on the ball can change the responsiveness of the system. Additionally, it can be helpful to spawn the player ball a bit above the maze's surface to ensure it does not fall through or stick to the surface.



Thanks for reading! If you have any suggestions for future Phidgets Education projects, let us know at


These materials are not sponsored by or affiliated with Unity Technologies or its affiliates. “Unity” is a trademark or registered trademark of Unity Technologies or its affiliates in the U.S. and elsewhere.