Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by DocteurCox · Nov 15, 2012 at 01:13 PM · c#androidinputgyroscope

Android Gyroscope not working ?

Hello there !

I'm currently working on an Android project and for the very first time I have to use Gyroscope. So far, touches have been working pretty well, the same does not go for Gyroscope though. In fact, whether I run my application through Unity Remote or build it to my phone (an Xperia U), it just does not work.

Here is my test code :

 using UnityEngine;
 using System.Collections;
 
 public class GyroscopeDataDisplayer : MonoBehaviour {
     
     Gyroscope m_gyroscope;
     
     // Use this for initialization
     void Start () {
         m_gyroscope = Input.gyro;
         m_gyroscope.enabled = true;
     }
     
     void OnGUI()
     {
         GUILayout.Label("Gyroscope attitude : " + m_gyroscope.attitude);    
         GUILayout.Label("Gyroscope gravity : " + m_gyroscope.gravity);    
         GUILayout.Label("Gyroscope rotationRate : " + m_gyroscope.rotationRate);    
         GUILayout.Label("Gyroscope rotationRateUnbiased : " + m_gyroscope.rotationRateUnbiased);    
         GUILayout.Label("Gyroscope updateInterval : " + m_gyroscope.updateInterval);    
         GUILayout.Label("Gyroscope userAcceleration : " + m_gyroscope.userAcceleration);
     }
 }
 

Am I doing something wrong ?

Thanks in advance for your help ! :)

Comment
Add comment
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
3
Best Answer

Answer by programmrzinc · Nov 15, 2012 at 02:33 PM

I believe you need to use the Input.acceleration command instead of the Input.gryo. I use it and it works fine.

 function Update () {
     var dir : Vector3 = Vector3.zero;
 
     // we assume that the device is held parallel to the ground
     // and the Home button is in the right hand
 
     // remap the device acceleration axis to game coordinates:
     //  1) XY plane of the device is mapped onto XZ plane
     //  2) rotated 90 degrees around Y axis
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
Comment
Add comment · Show 6 · 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 DocteurCox · Nov 15, 2012 at 02:55 PM 0
Share

Yup, tried that and it works fine.

Concerning gyroscope, I searched the technical specs of my Xperia U and... it doesn't have one : now that make sense ! I wanted to try some augmented reality stuff, whatever I'll just forget that idea and try to work on something else.

Thanks for your answer BTW :)

avatar image Bunny83 · Nov 15, 2012 at 02:58 PM 0
Share

Yes, i thought i'Ve read somewhere that the Gyroscope class only works on iPhone, but i can't remember where. We also just use Input.acceleration and it works pretty well.

As a side note: It seems that Unity (or android itself) rotates the vector according to the current screen-orientation when you build for Android. The iPad seems to have a fixed coordinate system, so when you play upside down, it doesn't flip automatically.

avatar image programmrzinc · Nov 16, 2012 at 02:07 AM 0
Share

You are welcome, @DocteurCox

avatar image ar4ta · Jan 04, 2014 at 10:17 PM -2
Share

Excuse me, can you help me, please ? Why your code is not working for me ?

I'm used @DocteurCox and @programmrzinc code but still not working for my Android, the script i paste in $$anonymous$$ain Camera, how i must do ? :(

avatar image tanoshimi ar4ta · Jan 04, 2014 at 10:26 PM 0
Share

Please don't post questions as answers. Start a new question and explain precisely what the problem is. "Not working for you". Why not? Error message? Black screen? Incorrect values?

avatar image ar4ta ar4ta · Jan 05, 2014 at 07:48 AM 0
Share

I want to make project like this http://www.youtube.com/watch?v=XN8x9sUHSC4 (time 0:50)

I used this script :

 var speed = 10.0;
 
 function Update () {
 var dir : Vector3 = Vector3.zero;
         
 dir.x = -Input.acceleration.y;
 dir.z = Input.acceleration.x;
         
 if (dir.sqr$$anonymous$$agnitude > 1)
 dir.Normalize();
         
 dir *= Time.deltaTime;
 transform.Translate (dir * speed);
 }

But it's only moved the camera, not rotated, how to if i want to make like that sample video ?

avatar image
14

Answer by taxvi · Sep 10, 2015 at 08:06 AM

Put this in your Start() function

 Input.gyro.enabled = true;



Comment
Add comment · Show 5 · 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 josephlarralde · Mar 23, 2016 at 11:10 AM 1
Share

Setting Input.gyro.enabled to true works for me on my old Xperia with Unity 5. I think this should be the accepted answer, as the accelerometer and the gyroscope are totally different sensors and may be used for different purposes (accelerometer gives translation acceleration information and gyroscope gives rotation speed information). Thanks @taxvi

avatar image Bunny83 josephlarralde · Mar 23, 2016 at 12:39 PM 0
Share

While this answer is correct now, it has no explanation on the issue. It just states to do something that's already in the question. The problem here was that Unity didn't support the gyroscope on android in the beginning, only on iOS. I can't remember which version introduced the support for Android. The question is quite old. Which answer gets accepted is up to the OP and the accepted answer helped the OP back then.

Any kind of information on the internet has to be checked for date. It's an old question with the correct old answer. The answer of taxvi doesn't make sense since it doesn't answer the question. Because if someone has the same problem (which means someone uses the same code as the OP) the answer doesn't help at all since they already have that line inside Start. This question and accepted answer belongs to a problem of the past that doesn't exist anymore in the current Unity version.

avatar image josephlarralde Bunny83 · Mar 23, 2016 at 01:54 PM 0
Share

Oh, sorry I read the question too fast and went directly to the answers. I didn't notice enabled was set to true in the question as it's written on two lines, and neither did I realize that there was a 3 years gap between the 2 answers. $$anonymous$$y apologies. Anyway, in my use case, replacing gyroscope by accelerometer is not an option, hence my comment. I'm pretty new to Unity and this is the first thread I found on the subject after a google search. I guess I should have read the docs before. Glad to see that it works as expected now.

Show more comments
avatar image Wenger95 · Oct 25, 2016 at 12:15 PM 0
Share

Hi, I add Input.gyro.enabled = true; on void start(), but I don't know why in void update() Input.gyro.attitude still return me (0,0,0,1). And I found out Input.gyro.enabled still equal false, $$anonymous$$y device is iphone6.

Quaternion Device$$anonymous$$ = Quaternion.identity;

void Start () { if (!Input.gyro.enabled) { Input.gyro.enabled = true; Device$$anonymous$$ = Input.gyro.attitude; } //Input.location.Start (); }

void Update () {

         Device$$anonymous$$ =Input.gyro.attitude;

         Debug.Log("The Device$$anonymous$$ X is "+Device$$anonymous$$[0]+" The Device$$anonymous$$ Y is "+Device$$anonymous$$[1]+" The Device$$anonymous$$ Z is "+Device$$anonymous$$[2]+" The Device$$anonymous$$ W is "+Device$$anonymous$$[3]);

}

I delete some code, but it shouldn't matter.

Can you help me?

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.

Follow this Question

Answers Answers and Comments

17 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 avatar image avatar image avatar image avatar image

Related Questions

Detect Gyroscope's rotation around a single axis? Like a car game. 0 Answers

How to check whether you have swiped upwards on a mobile device - c# 1 Answer

Unity android bluetooth controller disconnect doesn't make array empty 1 Answer

Distribute terrain in zones 3 Answers

NGUI UIInput issue with some Android devices (Virtual keyboard is OK, but no letters appears on textfield) 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