Unity Community


Page 2 of 5 FirstFirst 1234 ... LastLast
Results 21 to 40 of 91
  1. RHD RHD is offline

    Location
    London, UK
    Posts
    608
    This is wonderful! Thank you!
    I have to figure out how to add it to a character controller next!

    I think I'm getting something wrong somewhere though, I'm getting flickering weirdness in the background. It's like it's repeating the screen image in layers over each other. Something to do with the refresh/frame rate?
    Any ideas what's causing this?

    Thanks.
    Attached Images  
    Last edited by RHD; 11-27-2011 at 03:52 PM. Reason: better image
    Unity Pro - Mac


  2. Location
    Italy
    Posts
    31
    I would like to use this code for an AR app, but in this case would be useful to reset the Quaternion (or the eulers) to the magnetic compass heading (I know it is not accurate but better than have the cam oriented to East when it should be North...)
    Any idea how to do this?
    Gameccino | Running Pixel
    follow me @runningpixel (personal and Xblig stuffs) or @gameccino (corporate, iOS and Android stuffs)


  3. Location
    ěresundsregionen
    Posts
    465
    Hmmm - just tried building for iPad2 and iPhone 4S and the image is just static on the "Front Z+" part. Camera doesnt rotate at all.

    The Update() is called, bool is true and I can see the gyro.attitude values changing. Just doesnt change the camera.

    Any ideas?

    /Thomas
    ElectroCute * Smack Boxing * TouchWars * Monster Ball * Smack Hockey * Tactical Soldier
    Unity SmartFoxServer API * Unity Asset Server Commit Mailer
    Follow on twitter: http://twitter.com/thomas_h_lund


  4. Location
    Tokyo, Japan
    Posts
    1,293
    Worked on a very similar feature at the beginning of this year :

    http://itunes.apple.com/us/app/id428087225?mt=8

    Maybe this could give you some ideas...
    I heard they was supposed to release the SDK, but I really dunno if they did.... O_o...
    (3D arts & C#) - Portfolio - Facebook
    sama.van - Chronology : #iOS
    sama.van - Shiro : Website


  5. Location
    Clichy/Paris
    Posts
    31
    wow... great... giant... thanks very much phoberman for your script...


  6. Location
    UK
    Posts
    1
    Cheers for the script!

    Just tried this on android now 3.5 supports it and works great.

    just needed to change
    gyroBool = Input.isGyroAvailable;
    with
    gyroBool = SystemInfo.supportsGyroscope;


  7. Posts
    6

    What about PortraitUpsideDown and LandscapeRight?

    Hi

    The code works great, but it lacks support for ScreenOrientation.LandscapeRight and ScreenOrientation.PortraitUpsideDown.

    I can't get my head around how this works and would really appreciate if someone could tell me how to implement support for the missing screen orientations?? What should be in the ?? fileds below for the Vector3 and the Quaternion?

    Any help will be much appreciated.

    Regards
    Dan


    Code:  
    1. if (Screen.orientation == ScreenOrientation.LandscapeLeft)
    2. {
    3.     camParent.transform.eulerAngles = new Vector3(90,90,0);
    4.     rotFix = new Quaternion(0f,0f,0.7071f,0.7071f);
    5. }
    6. else if (Screen.orientation == ScreenOrientation.LandscapeRight)
    7. {
    8.     camParent.transform.eulerAngles = new Vector3(??,??,??);
    9.     rotFix = new Quaternion(??,??,??,??);
    10. }
    11. else if (Screen.orientation == ScreenOrientation.Portrait)
    12. {
    13.     camParent.transform.eulerAngles = new Vector3(90,180,0);
    14.     rotFix = new Quaternion(0f,0f,1f,0f);
    15. }
    16. if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
    17. {
    18.     camParent.transform.eulerAngles = new Vector3(??,??,??);
    19.     rotFix = new Quaternion(??,??,??,??);
    20. }


  8. Location
    South Norwalk, CT
    Posts
    13
    I'm in the same boat as Funder75. Any way to add support for LanscapeRight?


  9. Posts
    6
    Yes, I actually did find the solution for LandscapeRight. Unfortunatly I'm stuck on the PortraitUpsideDown!


    Code:  
    1. else if (Screen.orientation == ScreenOrientation.LandscapeRight)
    2.  
    3. {
    4.  
    5.     camParent.transform.eulerAngles = new Vector3(90,90,0);
    6.  
    7.     rotFix = new Quaternion(0f, 0f, -0.7071f, 0.7071f);
    8.  
    9. }


  10. Posts
    6

    Success!

    With a lot of trial'n'error, some luck and a brief moment of clarity, I've succeeded in making all orientations work.
    I guess the code needs some cleaning up, but here it is...


    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GyroCam : MonoBehaviour
    5. {
    6.    
    7.     public CamManager target;
    8.     private bool gyroBool;
    9.     private Gyroscope gyro;
    10.     private Quaternion rotFix; 
    11.    
    12.     private GameObject camParent;
    13.     public ScreenOrientation currentOrientation;
    14.    
    15.     private bool initialized = false;
    16.     private Vector3 inititialRotation = new Vector3(90,90,0);
    17.    
    18.  
    19.    
    20.     private void init()
    21.     {
    22.         if (!initialized)
    23.         {
    24.             Transform originalParent = transform.parent; // check if this transform has a parent
    25.             camParent = new GameObject ("camParent"); // make a new parent
    26.             camParent.transform.position = transform.position; // move the new parent to this transform position
    27.             transform.parent = camParent.transform; // make this transform a child of the new parent
    28.             camParent.transform.parent = originalParent; // make the new parent a child of the original parent
    29.            
    30.             gyroBool = SystemInfo.supportsGyroscope;
    31.            
    32.             if (gyroBool) {
    33.                
    34.                 gyro = Input.gyro;
    35.                 gyro.enabled = true;
    36.                
    37.             }
    38.             else
    39.             {
    40.                 print("NO GYRO");
    41.             }
    42.            
    43.             initialized = true;
    44.         }
    45.     }
    46.    
    47.     private void updateScreenOrientation()
    48.     {
    49.         if (Screen.orientation == ScreenOrientation.LandscapeLeft)
    50.         {
    51.             camParent.transform.eulerAngles = inititialRotation;
    52.             rotFix = new Quaternion(0f,0f,0.7071f,0.7071f);
    53.         }
    54.         else if (Screen.orientation == ScreenOrientation.LandscapeRight)
    55.         {
    56.             camParent.transform.eulerAngles = inititialRotation;
    57.             rotFix = new Quaternion(0f,0f,-0.7071f,0.7071f);
    58.         }
    59.         else if (Screen.orientation == ScreenOrientation.Portrait)
    60.         {
    61.             camParent.transform.eulerAngles = inititialRotation;
    62.             rotFix = new Quaternion(0f,0f,1f,0f);
    63.         }
    64.         if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
    65.         {
    66.             camParent.transform.eulerAngles = inititialRotation;
    67.             rotFix = new Quaternion(0f,0f,0f,1f);
    68.         }
    69.        
    70.                
    71.     }
    72.    
    73.     void Update ()
    74.     {
    75.         init();
    76.         if (currentOrientation != Screen.orientation)
    77.         {
    78.             currentOrientation = Screen.orientation;
    79.             updateScreenOrientation();
    80.         }      
    81.        
    82.         if (gyroBool)
    83.         {
    84.             Quaternion camRot = gyro.attitude * rotFix;
    85.             transform.localRotation = camRot;
    86.             target.SetRotation(transform.rotation);
    87.         }
    88.         else
    89.         {
    90.             target.SetRotation(Quaternion.identity);
    91.         }
    92.  
    93.     }
    94. }


  11. Posts
    69
    hi all -

    Thanks for all the kind words. I forgot to check this thread for a while but I'm glad the script has proven to be so popular and useful.

    In the meantime I've been working with Android as well as iOS, and I had to do a little work to get the code working on both.

    Here's my current script, works on both my phones (iPhone4 and Galaxy Nexus) - still haven't tackled anything except LandscapeLeft but now maybe I'll get around to it -


    Code:  
    1. private var gyroBool : boolean;
    2. private var gyro : Gyroscope;
    3. private var quatMult : Quaternion;
    4. private var quatMap : Quaternion;
    5.  
    6. function Start() {
    7.     // find the current parent of the camera's transform
    8.     var currentParent = transform.parent;
    9.     // instantiate a new transform
    10.     var camParent = new GameObject ("camParent");
    11.     // match the transform to the camera position
    12.     camParent.transform.position = transform.position;
    13.     // make the new transform the parent of the camera transform
    14.     transform.parent = camParent.transform;
    15.     // make the original parent the grandparent of the camera transform
    16.     camParent.transform.parent = currentParent;
    17.     // check whether device supports gyroscope
    18.     #if UNITY_3_4
    19.     gyroBool = Input.isGyroAvailable;
    20.     #endif
    21.     #if UNITY_3_5
    22.     gyroBool = SystemInfo.supportsGyroscope;
    23.     #endif
    24.    
    25.     if (gyroBool) {
    26.         gyro = Input.gyro;
    27.         gyro.enabled = true;
    28.         #if UNITY_IPHONE
    29.             camParent.transform.eulerAngles = Vector3(90,90,0);
    30.             quatMult = Quaternion(0,0,0.7071,0.7071);
    31.         #endif
    32.         #if UNITY_ANDROID
    33.             camParent.transform.eulerAngles = Vector3(-90,0,0);
    34.             quatMult = Quaternion(0, 0, 0.7071, -0.7071);
    35.         #endif
    36.         Screen.sleepTimeout = 0;
    37.     } else {
    38.         #if UNITY_EDITOR
    39.             print("NO GYRO");
    40.         #endif
    41.     }
    42. }
    43.  
    44. function Update () {
    45.     if (gyroBool) {
    46.         #if UNITY_IPHONE
    47.             quatMap = gyro.attitude;
    48.         #endif
    49.         #if UNITY_ANDROID
    50.             quatMap = Quaternion(gyro.attitude.w,gyro.attitude.x,gyro.attitude.y,gyro.attitude.z);
    51.         #endif
    52.         transform.localRotation = quatMap * quatMult;
    53.     }
    54. }


  12. Posts
    69

    Update: now works with all orientations on iOS & Android

    Here's an update that covers all orientations - for both iOS & Android

    Code:  
    1. #pragma strict
    2. // Gyroscope-controlled camera for iPhone & Android revised 2.26.12
    3. // Perry Hoberman <hoberman@bway.net>
    4. //
    5. // Usage:
    6. // Attach this script to main camera.
    7. // Note: Unity Remote does not currently support gyroscope.
    8. //
    9. // This script uses three techniques to get the correct orientation out of the gyroscope attitude:
    10. // 1. creates a parent transform (camParent) and rotates it with eulerAngles
    11. // 2. for Android (Samsung Galaxy Nexus) only: remaps gyro.Attitude quaternion values from xyzw to wxyz (quatMap)
    12. // 3. multiplies attitude quaternion by quaternion quatMult
    13.  
    14. // Also creates a grandparent (camGrandparent) which can be rotated with localEulerAngles.y
    15. // This node allows an arbitrary heading to be added to the gyroscope reading
    16. // so that the virtual camera can be facing any direction in the scene, no matter what the phone's heading
    17.  
    18. static var gyroBool : boolean;
    19. private var gyro : Gyroscope;
    20. private var quatMult : Quaternion;
    21. private var quatMap : Quaternion;
    22.  
    23. function Awake() {
    24.     // find the current parent of the camera's transform
    25.     var currentParent = transform.parent;
    26.     // instantiate a new transform
    27.     var camParent = new GameObject ("camParent");
    28.     // match the transform to the camera position
    29.     camParent.transform.position = transform.position;
    30.     // make the new transform the parent of the camera transform
    31.     transform.parent = camParent.transform;
    32.     // make the original parent the grandparent of the camera transform
    33.     //camParent.transform.parent = currentParent;
    34.     // instantiate a new transform
    35.     var camGrandparent = new GameObject ("camGrandParent");
    36.     // match the transform to the camera position
    37.     camGrandparent.transform.position = transform.position;
    38.     // make the new transform the parent of the camera transform
    39.     camParent.transform.parent = camGrandparent.transform;
    40.     // make the original parent the grandparent of the camera transform
    41.     camGrandparent.transform.parent = currentParent;
    42.        
    43.     // check whether device supports gyroscope
    44.     #if UNITY_3_4
    45.     gyroBool = Input.isGyroAvailable;
    46.     #endif
    47.     #if UNITY_3_5
    48.     gyroBool = SystemInfo.supportsGyroscope;
    49.     #endif
    50.    
    51.     if (gyroBool) {
    52.         gyro = Input.gyro;
    53.         gyro.enabled = true;
    54.         #if UNITY_IPHONE
    55.             camParent.transform.eulerAngles = Vector3(90,90,0);
    56.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    57.                 quatMult = Quaternion(0f,0,0.7071,0.7071);
    58.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    59.                 quatMult = Quaternion(0,0,-0.7071,0.7071);
    60.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    61.                 quatMult = Quaternion(0,0,1,0);
    62.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    63.                 quatMult = new Quaternion(0,0,0,1);
    64.             }
    65.         #endif
    66.         #if UNITY_ANDROID
    67.             camParent.transform.eulerAngles = Vector3(-90,0,0);
    68.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    69.                 quatMult = Quaternion(0f,0,0.7071,-0.7071);
    70.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    71.                 quatMult = Quaternion(0,0,-0.7071,-0.7071);
    72.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    73.                 quatMult = Quaternion(0,0,0,1);
    74.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    75.                 quatMult = new Quaternion(0,0,1,0);
    76.             }
    77.         #endif
    78.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
    79.     } else {
    80.         #if UNITY_EDITOR
    81.             //print("NO GYRO");
    82.         #endif
    83.     }
    84. }
    85.  
    86. function Update () {
    87.     if (gyroBool) {
    88.         #if UNITY_IPHONE
    89.             quatMap = gyro.attitude;
    90.         #endif
    91.         #if UNITY_ANDROID
    92.             quatMap = Quaternion(gyro.attitude.w,gyro.attitude.x,gyro.attitude.y,gyro.attitude.z);
    93.         #endif
    94.         transform.localRotation = quatMap * quatMult;
    95.     }
    96. }


  13. Location
    Lost in Space....................*_*_*_....
    Posts
    298
    Man Thanks Perry for the share !!!! and updated Code....
    i did update it myself and thought to post it here but you are faster and your Code look's more polished
    anyway many thanks for sharing.... in this times it is not often that this happen
    i really miss the good old time here in the forum where help & sharing was normal...
    Life's What You Make It - don't forget it's YOU


  14. Posts
    21
    Wow really nice share.

    Does this also work on the Galaxy Tab? Because i don't have the right input..
    My screen is always rotated 90 degrees. Is this because it is build for phone?


  15. Posts
    69
    Here's another update - I added basic touch input so that a horizontal swipe rotates the heading.

    Black Spider, I would just experiment with the setting for camParent.transform.eulerAngles - try different rotations in 90 degree increments.

    By the way, this script is part of my very first package for the Asset Store - which just got approved!
    It's called FOV2GO and it's a comprehensive stereo 3D toolkit.
    Did I mention that it's FREE?
    Check it out: http://u3d.as/content/stereoskopix/fov2go

    pH


    Code:  
    1. #pragma strict
    2. // Gyroscope-controlled camera for iPhone & Android revised 2.26.12
    3. // Stereoskopix FOV2GO Copyright (c) 2011 Perry Hoberman
    4. // Perry Hoberman <hoberman@bway.net>
    5. //
    6. // Usage:
    7. // Attach this script to main camera.
    8. // Note: Unity Remote does not currently support gyroscope.
    9. // Use Landscape Left for correct orientation
    10. //
    11. // This script uses three techniques to get the correct orientation out of the gyroscope attitude:
    12. // 1. creates a parent transform (camParent) and rotates it with eulerAngles
    13. // 2. for Android (Samsung Galaxy Nexus) only: remaps gyro.Attitude quaternion values from xyzw to wxyz (quatMap)
    14. // 3. multiplies attitude quaternion by quaternion quatMult
    15.  
    16. // Also creates a grandparent (camGrandparent) which can be rotated to change heading
    17. // This node allows an arbitrary heading to be added to the gyroscope reading
    18. // so that the virtual camera can be facing any direction in the scene, no matter which way the phone is actually facing
    19. // Option for touch input - horizontal swipe controls heading
    20.  
    21. static var gyroBool : boolean;
    22. private var gyro : Gyroscope;
    23. private var quatMult : Quaternion;
    24. private var quatMap : Quaternion;
    25. // camera grandparent node to rotate heading
    26. private var camGrandparent : GameObject;
    27. private var heading : float = 0;
    28.  
    29. // mouse/touch input
    30. public var touchRotatesHeading : boolean = true;
    31. private var screenSize : Vector2;
    32. private var mouseStartPoint: Vector2;
    33. private var headingAtTouchStart : float = 0;
    34. @script AddComponentMenu ("stereoskopix/s3d Gyro Cam")
    35.  
    36. function Awake() {
    37.     // find the current parent of the camera's transform
    38.     var currentParent = transform.parent;
    39.     // instantiate a new transform
    40.     var camParent = new GameObject ("camParent");
    41.     // match the transform to the camera position
    42.     camParent.transform.position = transform.position;
    43.     // make the new transform the parent of the camera transform
    44.     transform.parent = camParent.transform;
    45.     // instantiate a new transform
    46.     camGrandparent = new GameObject ("camGrandParent");
    47.     // match the transform to the camera position
    48.     camGrandparent.transform.position = transform.position;
    49.     // make the new transform the grandparent of the camera transform
    50.     camParent.transform.parent = camGrandparent.transform;
    51.     // make the original parent the great grandparent of the camera transform
    52.     camGrandparent.transform.parent = currentParent;
    53.        
    54.     // check whether device supports gyroscope
    55.     #if UNITY_3_4
    56.     gyroBool = Input.isGyroAvailable;
    57.     #endif
    58.     #if UNITY_3_5
    59.     gyroBool = SystemInfo.supportsGyroscope;
    60.     #endif
    61.    
    62.     if (gyroBool) {
    63.         gyro = Input.gyro;
    64.         gyro.enabled = true;
    65.         #if UNITY_IPHONE
    66.             camParent.transform.eulerAngles = Vector3(90,90,0);
    67.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    68.                 quatMult = Quaternion(0,0,0.7071,0.7071);
    69.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    70.                 quatMult = Quaternion(0,0,-0.7071,0.7071);
    71.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    72.                 quatMult = Quaternion(0,0,1,0);
    73.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    74.                 quatMult = Quaternion(0,0,0,1);
    75.             }
    76.         #endif
    77.         #if UNITY_ANDROID
    78.             camParent.transform.eulerAngles = Vector3(-90,0,0);
    79.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    80.                 quatMult = Quaternion(0,0,0.7071,-0.7071);
    81.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    82.                 quatMult = Quaternion(0,0,-0.7071,-0.7071);
    83.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    84.                 quatMult = Quaternion(0,0,0,1);
    85.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    86.                 quatMult = Quaternion(0,0,1,0);
    87.             }
    88.         #endif
    89.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
    90.     } else {
    91.         #if UNITY_EDITOR
    92.             //print("NO GYRO");
    93.         #endif
    94.     }
    95. }
    96.  
    97. function Start() {
    98.     screenSize.x = Screen.width;
    99.     screenSize.y = Screen.height;
    100. }
    101.  
    102. function Update () {
    103.     if (gyroBool) {
    104.         #if UNITY_IPHONE
    105.             quatMap = gyro.attitude;
    106.         #endif
    107.         #if UNITY_ANDROID
    108.             quatMap = Quaternion(gyro.attitude.w,gyro.attitude.x,gyro.attitude.y,gyro.attitude.z);
    109.         #endif
    110.         transform.localRotation = quatMap * quatMult;
    111.     }
    112.     #if (UNITY_IPHONE || UNITY_ANDROID) && !UNITY_EDITOR
    113.         if (touchRotatesHeading) {
    114.             GetTouchMouseInput();
    115.         }
    116.         camGrandparent.transform.localEulerAngles.y = heading;
    117.     #endif
    118. }
    119.  
    120. function GetTouchMouseInput() {
    121.     if(Input.GetMouseButtonDown(0)) {
    122.         mouseStartPoint = Input.mousePosition;
    123.         headingAtTouchStart = heading;
    124.     } else if (Input.GetMouseButton(0)) {
    125.         var delta : Vector2;
    126.         var mousePos = Input.mousePosition;
    127.         delta.x = (mousePos.x - mouseStartPoint.x)/screenSize.x;
    128.         delta.y = (mousePos.y - mouseStartPoint.y)/screenSize.y;
    129.         heading = (headingAtTouchStart+delta.x*100);
    130.         heading = heading%360;
    131.     }
    132. }


  16. Location
    Denmark
    Posts
    38
    Great script! But how did you calculate which values to use for the rotation fix and why is it only necessary to calibrate in the Awake method and not Update?

    I noticed that it would always add 90 degrees to the Z rotation of the child camera, so I tried doing only this and still getting the same results as before:

    Code:  
    1. transform.parent.transform.eulerAngles = new Vector3 (90, 90, 0);
    2. Quaternion currentGyro = Input.gyro.attitude;
    3. transform.localRotation = currentGyro * Quaternion.Euler(0, 0, 90);
    Last edited by MikeWG; 03-21-2012 at 08:11 AM.


  17. Location
    Halifax, NS
    Posts
    76
    Thanks for the great script! Works like a charm on iOS, but I'm having some trouble on Android. Objects seem to drift with the camera, especially when moving side to side. I'm running in landscape left. Seems to be much better on the Galaxy Nexus, but other devices (Nexus S, Galaxy SII, etc) have terrible drift. Any ideas what might be causing this?

    Is Unity maybe faking the gyro input with accelerometer inputs?


  18. Posts
    69
    I would guess that the drift has more to do with the phone itself - drivers, OS, or gyroscope hardware - than with Unity.

    I was getting terrible results (probably similar to yours) on the LG Thrill 4G running Android 2.3 - just upgraded to Gingerbread - now the gyro works perfectly.


  19. Posts
    446
    First, phoberman, thanks so much for sharing...really a fun tool.

    I have a quick question though. Is there anyway to reset the camera back to a pre-defined location and then lock its location?

    I'm exploring something that I work with a couple of different cameras. And when I flip on a gyro camera (with your script), the location of the camera is always weird (as though the iPad's gyroscope has been keeping track of the camera all along and then passing that along to the gyro camera when its activated). I really want to be able to have that gyro camera stay in one place, but be able to look around from that fixed point....

    So thankful for any guidance or suggestions you could provide.


  20. Location
    Toronto, ON, Canada
    Posts
    5
    nm8shun/ I am trying (relatively) the same thing myself and started a thread here, and here but no one seems to be interested so I will ask phoberman the same thing here...

Page 2 of 5 FirstFirst 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •