Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[sharing] gyroscope-controlled camera on iPhone 4

Discussion in 'iOS and tvOS' started by phoberman, Aug 1, 2011.

  1. hamokshaelzaki

    hamokshaelzaki

    Joined:
    Nov 6, 2012
    Posts:
    19
    You saved us!! How can we repay you?
     
  2. wbknox

    wbknox

    Joined:
    Aug 1, 2016
    Posts:
    11
    Possibly helpful: I've written a post in Unity Answers, which offers a solution to a related problem and points to another forum thread with an alternate solution to using the gyro to control a camera such that it mimics the phone's camera. I found this other solution relatively easier to follow.

    My post: http://answers.unity3d.com/questions/1265974/finding-physical-world-xyz-with-respect-to-an-ios.html
    Alternate solution: https://forum.unity3d.com/threads/sharing-gyroscope-camera-script-ios-tested.241825/
     
  3. HanSoloYolo

    HanSoloYolo

    Joined:
    May 22, 2017
    Posts:
    19
    The drifting issue is caused by the camera being at an offset Y rotation from the camParent. Rotate your Y axis on you main cam in 90 degree increments until the issue is fixed. Once it is aligned with your cam-Parent object, it will stop drifting no matter what view you have it in.

    I really need help with the following, however. I have a script to look around with handheld vr, but want to GetTouch.Input == 2 in order to reset the orientation to the current gyro.attitude. I am extremely new to Unity and C# in general, any help adding 2-touch to center the camera based on the current gyro.attitude and/or offset it from the original gyro.attitude, Quaternion.identity, or whatever it is that I actually need. Any help would be supremely appreciated.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. public class GyroController : MonoBehaviour
    4. {
    5.     private bool gyroEnabled;
    6.     private Gyroscope gyro;
    7.     private GameObject GyroControl;
    8.     private Quaternion rot;
    9.  
    10.     private void Start ()
    11.     {
    12.         GyroControl = new GameObject ("Gyro Control");
    13.         GyroControl.transform.position = transform.position;
    14.         transform.SetParent (GyroControl.transform);
    15.         gyroEnabled = EnableGyro();
    16.  
    17.     }
    18.  
    19.  
    20.     private bool EnableGyro()
    21.     {
    22.         if (SystemInfo.supportsGyroscope)
    23.             {
    24.             gyro = Input.gyro;
    25.             gyro.enabled = true;
    26.             GyroControl.transform.rotation = Quaternion.Euler(90f, -90f, 0f);
    27.             rot = new Quaternion(0, 0, 1, 0);
    28.             return true;
    29.             }
    30.         return false;
    31.     }
    32.  
    33.     private void Update ()
    34.     {
    35.         if (gyroEnabled)
    36.         {
    37.             transform.localRotation = gyro.attitude * rot;
    38.         }
    39.     }
    40. }
     
    Last edited: Jul 23, 2017
  4. HanSoloYolo

    HanSoloYolo

    Joined:
    May 22, 2017
    Posts:
    19
    On this line:
    Code (CSharp):
    1. GyroControl.transform.rotation = Quaternion.Euler(90f, -90f, 0f);
    I suggest changing the values of (90f, __f, __f); as needed to match your scene to your liking and to solve any drifting problems you may have. You may notice that even though you set it to Landscape Left, it drifts, but turn your phone to Landscape Right and it may not drift. If this or something similar is the case, changing these values will solve it. With a little trial and error. =)
     
    Last edited: Jul 23, 2017
  5. daneyuleb

    daneyuleb

    Joined:
    Dec 19, 2017
    Posts:
    3
    Love this script, but can anyone give a hint on how to clamp the rotation on a given axis to an angle range? I need to restrict the motion both up and down to a limited range. Vector3's are easy, but trying to figuroe out how to clamp quaternions is about to make my head explode.