Orientation Tracking with Unity

Learn how to use the built-in functions of the Spatial Phidget with Unity!

Prerequisites

This project assumes you are familiar with the basic functionality of Unity.

Setup

Note: You must use the Spatial Phidget (MOT1102) for this project. Other Spatial Phidgets (e.g. MOT1101) do not have the built-in functionality required.

Spatial Phidget

Phidget cable

USB cable

Attach your Spatial Phidget to your VINT Hub as shown:

Configure Unity

In order to use Phidgets with Unity, you have to configure your project. Follow the instructions below.

Step 1

Download a copy of the Phidget libraries and unzip it as shown.

Step 2

Next, open Unity and create a new 3D project. Name it Phidgets_Unity and place it in the location of your choice.

Step 3

Navigate to the file you previously unzipped and import Phidget22.NET.dll and phidget22.dll as shown.

Note: If you are developing on macOS, you do not need to import phidget22.dll

Create Environment

Now that your project is configured, you can create your environment.

Step 1

Download the free Star Sparrow Modular Spaceship asset and add it to your project.

Step 2

Navigate to Assets > StarSparrow > Prefabs and add any of the spaceships to your scene. Set the x, y and z position of your spaceship to 0.

Step 3

Select the spaceship and add a Rigidbody component by clicking on Component > Physics > Ridigbody.

Step 4

With the spaceship selected, uncheck the Use Gravity property.

Step 5

Finally, add a script to the spaceship called SpaceScript

Write Code (C#)

Copy the code below into your SpaceScript file.

  
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Phidget22;



public class SpaceScript : MonoBehaviour
{
    private Rigidbody rb;
    Spatial spatial;

    // Start is called before the first frame update
    void Start()
    {
        //Connect Rigidbody
        rb = GetComponent <Rigidbody>();

        //Create
        spatial = new Spatial();

        //Subscribe to Event
        spatial.AlgorithmData += Spatial_AlgorithmData;

        //Open
        spatial.Open(1000);

        //Set data interval to minimum 
        spatial.DataInterval = spatial.MinDataInterval;
    }

    double[] q;
    void Spatial_AlgorithmData(object sender, Phidget22.Events.SpatialAlgorithmDataEventArgs e) {
        q = e.Quaternion;
	}

    // Update is called once per frame
    void Update()
    {
        if (q != null)
        {            
            rb.MoveRotation(new Quaternion((float)q[0], (float)(-1.0 *q[2]), (float)q[1], (float)q[3]));
        }
    }

    //Required for Unity and Phidgets
    private void OnApplicationQuit()
    {
        spatial.Close();
        spatial.Dispose();
    }
}
  

Run Your Program

When you move your Spatial Phidget, the spaceship will mirror the movement!

Code Review

When using the Spatial Phidget, it's important to set the data interval to the minimum as shown in the code above. This will ensure your sensor is as responsive as possible.

The Spatial Phidget returns a quaternion that you can use directly with Unity. Learn more about quaternions here.

Practice

  1. Try adding a dynamic space background to your Unity Project
  2. Try adding gravity and movement to your spaceship.

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done