• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by 3 · Mar 09, 2013 at 02:44 AM · iosmobileaccelerometergyroscopegyro

New Gyroscope Controls in Unity 4?

WHY UNITY WHY? I had just got t$$anonymous$$s script for a gyroscope controlled main camera online, and it was working well. Maybe .1 second lag with the gyroscope, but it was working. I had just made a few edits, otherwise it was the original. When I used the same script in a different, the gyroscope is basically messed up. It flipped the game 90 degrees, and when I tilt it upwards, it moves the camera left. When I tilt it downwards, it goes right. When I move it right, it goes up, and vice versa. Any help would be really really greatly appreciated. I'm stumped, the only change I've made is upgraded to unity 4, maybe that could have somet$$anonymous$$ng to do with it? Thanks,

Here is the script by the way:

 // iPhone gyroscope-controlled camera demo v0.3 8/8/11
 // Perry Hoberman <hoberman@bway.net>
 // Directions: Attach t$$anonymous$$s script to main camera.
 // Note: Unity Remote does not currently support gyroscope. 
 
 private var gyroBool : boolean;
 private var gyro : Gyroscope;
 private var rotFix : Quaternion;
 
 function Start() {
     
     var originalParent = transform.parent; // check if t$$anonymous$$s transform has a parent
     var camParent = new GameObject ("camParent"); // make a new parent
     camParent.transform.position = transform.position; // move the new parent to t$$anonymous$$s transform position
     transform.parent = camParent.transform; // make t$$anonymous$$s transform a c$$anonymous$$ld of the new parent
     camParent.transform.parent = originalParent; // make the new parent a c$$anonymous$$ld of the original parent
     
     gyroBool = Input.isGyroAvailable;
     
     if (gyroBool) {
         
         gyro = Input.gyro;
         gyro.enabled = true;
         
         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
             camParent.transform.eulerAngles = Vector3(90,90,0);
         } else if (Screen.orientation == ScreenOrientation.Portrait) {
             camParent.transform.eulerAngles = Vector3(90,180,0);
         }
         
         if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
             rotFix = Quaternion(0,0,0.7071,0.7071);
         } else if (Screen.orientation == ScreenOrientation.Portrait) {
             rotFix = Quaternion(0,0,1,0);
         }
         //Screen.sleepTimeout = 0;
     } else {
         print("NO GYRO");
     }
 }
 
 function Update () {
 Invoke ("Rotate", 12.8);
 }
 
 function Rotate () {
     if (gyroBool) {
         var camRot : Quaternion = gyro.attitude * rotFix;
         transform.localRotation = camRot;
     }
 }
Comment
ina

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by headhunter45 · Nov 02, 2013 at 06:37 AM

Once the gyro is returning good data do t$$anonymous$$s once to figure out the initial rotation

     inverseInitialRotation = Quaternion.Inverse(Input.gyro.attitude);

Then every frame after that

     Quaternion deltaQ = Input.gyro.attitude * inverseInitialRotation;
     transform.localRotation = Quaternion.Euler(-deltaQ.eulerAngles.y, -deltaQ.eulerAngles.z, deltaQ.eulerAngles.x);


You may have to swap the euler values differently depending on your camera's orientation in the scene.

Comment
tanoshimi

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Nov 02, 2013 at 07:41 AM 0
Share

"You may have to swap the euler values differently depending on your camera's orientation in the scene" - Yeah, that's the part that's the pain. And I find it also differs between iOS and Android.

Various suggestions include:

  • Rotate the gyro attitude reading by a constant quaternion (0,0,1,0) and rotate its parent object by a constant angle of (90,180,0) for all device orientations (Post from “Aroha” in http://forum.unity3d.com/threads/98828-sharing-gyroscope-controlled-camera-on-iPhone-4/page5)

  • Rotate the gyro attitude by eulerangles (90, 0, 90) (Post by “George Foot” in http://forum.unity3d.com/threads/128493-Match-Unity-camera-with-iPhone-camera)

  • Rotate the gyro attitude dependent on screen orientation – Z rotation of –90 for Landscape Left, +90 for Landscape Right, or 180 for Portrait Upside Down, and also invert the z and w coordinates of the attitude quaternion to be (x, y, -z, -w) (http://blog.heyworks.com/how-to-write-gyroscope-controller-with-unity3d/)

What worked for me was to invert the w and z aspects of the quaternion attitude, i.e.:

 rotFix = new Quaternion(gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
avatar image skinnysimmons · Aug 14, 2014 at 03:40 PM 0
Share

do you put this rotFix in for both in the code above? I'm still having issues with getting landScaleLeft to work properly. I've tried numerous rotation combinations in this part of the code, but to no avail...

camParent.transform.eulerAngles = Vector3(90,90,0);

anyone have values they KNOW work? I'm up for trying more :D

thanks in advance!!!

note: the calculation work properly in Portrait mode, whereby i can just turn my phone and then go from there, but my GUI is then off ;)

avatar image

Answer by Oneiros90 · Oct 28, 2015 at 01:21 PM

What worked for me (tested on iPhone 6 with Unity 5.2):

 Quaternion rotFix1 = Quaternion.Euler(90, 90, 0);
 Quaternion rotFix2 = Quaternion.Euler(0, 0, 180);
 camera.localRotation = rotFix1 * Input.gyro.attitude * rotFix2;
Comment
andre.hirohito

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NO Gyro on iPhone 4S 2 Answers

Accelerometer - accounting for gravity for arbitrary device angle 1 Answer

Using Gyroscope with Android 1 Answer

Match Object (Main Camera) To Gyroscope Rotation Rate 1 Answer

Sensor Fusion of Accelerometer and Gyroscope 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges