Sunday, May 18, 2014

Teensy 3.1 Accelerometer - Control a Game Object in Unity

Getting the accelerometer data from the Teensy is nice, but the WPF application with some lines on the screen was sort of boring. I wanted to do something more fun with the Teensy 3.1 Accelerometer - use it as a game controller!  So this project is all about using the Teensy 3.1 Accelerometer to control an airplane on the screen using the Unity game development suite.



Hardware Setup

  1. Follow the instructions to build the Teensy 3.1 Accelerometer. This will be used as the game controller through a  USB serial connection.
  2. Note: the Unity game expects the Accelerometer's x, y, and z axis' to be oriented with the Y-axis arrow pointing to the right of the user and the X-axis arrow pointing toward the user when the circuit is in use. 


Software Setup

  1. Download and install the Unity game development suite. (this takes a while, it's a big download)

Now lets take a get the Unity project working.

Step 1 - Download the Unity Plane Game project.

  1. Download the UnityPlaneGame.zip file and extract it to a UnityPlaneGame directory. 
  2. Create an Assets directory under the UnityPlaneGame directory that was just created on your local drive.
  3. Download everything in the UnityPlaneGame/Assets directory of the repository and to your Assets directory. 
  4. Extract the .7z compressed files to sub directories of the Assets directory that match the compressed file names. You will need 7-zip to extract the files. You can download 7-zip here.
  5. Copy the scripts directory and other files directly to the root of your Assets directory. Your Assets directory file structure should look as shown below. 

Step 2 - Open the Unity Project.


  1. Open the Unity Game development suite. 
  2. From the "File" menu, select "Open Project", then choose "Open Other" at the bottom left corner and browse to the UnityPlaneGame directory. Then click the "Open" button.
  3. The project should open and look something like the image below.

  4. Now Choose "Build and Run" from the "File" Menu to make sure the project will build. If not, verify the above steps.

Step 3 - Change the COM port to match your Teensy 3.1 Accelerometer

  1. From the Project window in Unity, navigate to the scripts directory and open the PlaneAnimator.cs script by double clicking on the file. The file will open with MonoDevelop. 
  2. In the script, change the COMPORT string from "COM6" to the COM port that your Teensy 3.1 Accelerometer is on and Save your changes.


Step 4 - Run the Game

  1. At this point, make sure your Teensy Accelerometer is plugged in. (The USB cable). The game is programmed to open the serial port only once on start up, so it will not work if you plug it in after the game is already running.
  2. Return the the Unity project window. 
  3. From the File menu, select Build and Run. A window will open with the Unity logo in it, and then the game scene will appear with the plane visible on the screen. Try using the Teensy accelerometer to control the plane. Tilting the accelerometer left and right should make the plane move left and right. Also try tilting it back with the front side higher than the backside. That should make the plane climb on the screen. Tilt it in the opposite direction to make the plane dive. See if you can find the buildings and fly through them! There are no collisions in the game so you can fly through anything! 



No comments:

Post a Comment