Prerequisites
This project assumes you are familiar with the basic functionality of Unity. You should also review the following before moving on:
Setup
Note: You can use any Phidget with an accelerometer for this project.
VINT Hub

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.
Write Code (C#)
Copy the code below into your BallScript file.
using UnityEngine;
using Phidget22;
public class BallScript : MonoBehaviour
{
private Rigidbody rb;
Accelerometer accelerometer;
// Start is called before the first frame update
void Start()
{
//Connect Rigidbody
rb = GetComponent <Rigidbody>();
//Create
accelerometer = new Accelerometer();
//Open
accelerometer.Open(1000);
//Set data interval to minimum | This will increase the data rate from the sensor and allow for smoother movement
accelerometer.DataInterval = accelerometer.MinDataInterval;
}
// Update is called once per frame
void Update()
{
//Use your Phidgets
float x = (float)accelerometer.Acceleration[0];
Vector3 movement = new Vector3(x, 0.0f, 0.0f);
rb.AddForce(movement);
}
//Required for Unity and Phidgets
private void OnApplicationQuit()
{
accelerometer.Close();
accelerometer.Dispose();
}
}
Run Your Program
When you tilt your accelerometer, the sphere will also move.
Code Review
When using the Accelerometer 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.
Practice
- Try to identify when the Accelerometer Phidget is being shaken and make the sphere jump when this occurs. Review the Dice Roll project for information.
- Instead of adding a force to your sphere based on the accelerometer, try mapping the acceleration value to the sphere directly
// Update is called once per frame
void Update()
{
//Use your Phidgets
float x = (float)accelerometer.Acceleration[0] * 4.0f;
rb.position = new Vector3(x, rb.position.y, rb.position.z);
}
3. Try using the other axes on the Accelerometer Phidget to control your sphere. For more information on how to access the y and z axis, visit the Accelerometer tutorial page.