Tuesday 6 November 2012

iOS Gyro Controller for Unity

Hi,


While playing around with Unity Remote, to control my game with my iPad, I noticed that the gyroscope doesn't work. This sucks, so I tried to find a solution. I found one from the guys of Quick Fingers, though it needed some tweaks, so I'm writing an updated version here. You can read the original tutorial here, mine will be shorter and more to the point.

Needed Soft/Hardware

  • Unity3D and a mac
  • An iOS device with Gyroscope (any new iPhone, iTouch or iPad)
  • Control for iOS
  • Osculator (trail, 20second wait every 10minutes)

Steps

  • Install and start  Osculator and open the QuickFingers file
  • On your iOS device open Control, in 'Interfaces' add a new one and enter this URL:  http://www.quickfingers.net/unitygyro/layout.js
  • In Unity setup your Input Settings like this: 

  • Now you can use this in Unity, here's how to convert it to a quaternion:
float gyroX = Input.GetAxisRaw("GyroX") * 360.0f;
float gyroY = Input.GetAxisRaw("GyroY") * 360.0f;
float gyroZ = Input.GetAxisRaw("GyroZ") * 360.0f;
Quaternion rotation = Quaternion.Euler(gyroX, gyroY, gyroZ);

Now you can use rotation as the rotation of your camera yo create a window into your game. Kind of, since no image is send to the iPad... Still, it's great for testing and a lot of fun!

2 comments:

  1. Hi, I was searching for an alternative to QuickFinger's tutorial, because for some reason it doesn't work for me. I am still using unity very basically, so I still don't understand much about it. Where do you put that piece of quaternion script you wrote here?
    Thanks

    ReplyDelete
    Replies
    1. The quaternion represents the rotations of your ipad/iphone. So you can assign it to anything you want to have the same rotation. Most of the time you'll want to do: "transform.rotation = rotation;" in your Update. If you do that in a script attached to your camera object, it'll make the camera rotate the same way your device is, and by that it will look like you are looking through your device into your world.
      So, it depends on what you want to do. I would suggest first trying the camera thing I just described, it's very cool! Then play around with it a bit :D

      Delete