Advertisement
rhose87

iPhoneGyroCam

Feb 6th, 2012
4,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // iPhone gyroscope-controlled camera demo v0.3 8/8/11
  2. // Perry Hoberman <hoberman@bway.net>
  3. // Directions: Attach this script to main camera.
  4. // Note: Unity Remote does not currently support gyroscope.
  5.  
  6. private var gyroBool : boolean;
  7. private var gyro : Gyroscope;
  8. private var rotFix : Quaternion;
  9. public var fpc : GameObject;
  10.  
  11. function Start() { 
  12.  
  13.     Screen.orientation  = ScreenOrientation.LandscapeLeft;
  14.        
  15.     var originalParent = transform.parent; // check if this transform has a parent
  16.     var camParent = new GameObject ("camParent"); // make a new parent
  17.     camParent.transform.position = transform.position; // move the new parent to this transform position
  18.     transform.parent = camParent.transform; // make this transform a child of the new parent
  19.     camParent.transform.parent = originalParent; // make the new parent a child of the original parent
  20.    
  21.     gyroBool = Input.isGyroAvailable;
  22.    
  23.     if (gyroBool) {
  24.  
  25.         gyro = Input.gyro;
  26.         gyro.enabled = true;
  27.        
  28.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
  29.             camParent.transform.eulerAngles = Vector3(90,90,0);
  30.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
  31.             camParent.transform.eulerAngles = Vector3(90,180,0);
  32.         }
  33.        
  34.         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
  35.             rotFix = Quaternion(0,0,0.7071,0.7071);
  36.         } else if (Screen.orientation == ScreenOrientation.Portrait) {
  37.             rotFix = Quaternion(0,0,1,0);
  38.         }      
  39.     } else {
  40.         print("NO GYRO");
  41.     }
  42. }
  43.  
  44. function Update () {
  45.     if (gyroBool) {
  46.         var camRot : Quaternion = gyro.attitude * rotFix;
  47.         transform.localRotation = camRot;
  48.         var direction : Vector3 = camRot * Vector3.up;
  49.         direction.y = 0;
  50.         gameObject.Find("print").guiText.text = direction.ToString();
  51.         //fpc.transform.rotation = Quaternion.LookRotation(direction);
  52.         }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement