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

Unity and Phidgets - Windows

Learn how to add Phidgets to your Unity applications!


by Phidgets Education

Introduction

Now that you've completed the Getting Started Kit and have a basic understanding of programming with Phidgets, you can easily integrate sensors into your Unity projects.

Prerequisites

This project assumes you have an understanding of Unity and have completed the Getting Started Kit.

For more information, check out the Student Page.


Hardware

You will need:

  • A Getting Started Kit

Setup:

Connect your Getting Started Kit to your computer.


Add the Phidgets Library


Start by creating a new 3D project named Phidgets_Unity_Test at the location of your choice.


Navigate to your project and right-click in the Assets window to select Import New Asset:


Go to C:/Program Files/Phidgets/Phidget22/ and import Phidget22.NET.dll from the list:


You've now successfully added the libraries to your project. The next step is creating your environment.

Note: if you are using an older version of Unity, check here for information on how to add the libraries.


Create Your Environment


Start by creating a 3D plane.


Next, add a sphere.


Adjust the sphere's Y-position to 0.5 so it sits on top of the plane.


Add a Rigidbody component to the sphere.


Next, add a script to the sphere called BallScript.


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


Writing Code


The first step is to open your BallScript file and add a reference to Phidget22.

Note: If you get an error at this point, make sure you have added the Phidgets library properly (shown above).


Next, create a DigitalInput object for your button, as well as a Rigidbody component for your sphere.


Add a StateChange event for your button like you've done in previous projects. Also add a boolean called buttonState to track whether the button has been pressed or not.


Next, modify your Update method so the ball jumps when the button is pressed.


Finally, override the OnApplicationQuit method and close your Phidget object.


Try running your program. You should see your sphere jump when the button is pushed.


Conclusion

Now that you know how to use Phidgets with Unity, try creating your own project with the Phidgets in your classroom. If you need inspiration, check out the links below: