Distance Phidget with Unity

Learn how to use the Distance Phidget with Unity!

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 capable of sensing distance for this project.

Distance Phidget

USB cable

Phidget cable

Attach your Distance 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

Create a 3D plane.

Step 2

Next, create a Sphere.

Step 3

Select the sphere and adjust the y-position to 0.5 so it sits on top of the plane.

Step 4

With the sphere selected, add a Rigidbody component.

Step 5

Next, add a script to the sphere called BallScript

Step 6

Finally, add some color to the plane to differentiate the objects.

Write Code (C#)

Copy the code below into your BallScript file.

  
using UnityEngine;
using Phidget22;

public class BallScript : MonoBehaviour
{

    private Rigidbody rb;
    DistanceSensor distanceSensor;

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

        //Create
        distanceSensor = new DistanceSensor();

        //Open
        distanceSensor.Open(1000);

        //Set data interval to minimum | This will increase the data rate from the sensor and allow for smoother movement
        distanceSensor.DataInterval = distanceSensor.MinDataInterval;
    }

    // Update is called once per frame
    void Update()
    {

        //Use your Phidgets
        float y = mapDistance();
        rb.position = new Vector3(rb.position.x, y, rb.position.z); 
    }

    float mapDistance(){
        try{
            float val = distanceSensor.Distance;
            float minD = distanceSensor.MinDistance;
            float maxD = distanceSensor.MaxDistance;
            maxD /= 5;
            float temp = ((val - minD)/(maxD - minD)) * 10.0f; 
            Debug.Log(temp);
            return  temp;
        }catch(PhidgetException ex){
            return 0.5f;
        }
    }

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

Run Your Program

When you move your hand above the Distance Phidget, the movement will be mapped to the sphere.

Code Review

When using the Distance 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

  1. Try modifying the definition of maxD in the mapDistance method. Instead of diving by 5, try using 1, 2, and 10. What are the results?

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