- Home /
I fixed it myself. Unity doesn't provide access to the data.
Android gyroscope
Ok, I just started using Unity Android mistakingly believing that Input.gyro would get me data from the gyroscope on a Galaxy Tab, but it doesn't.
I found this post
http://answers.unity3d.com/questions/43794/compass-java-plugin-for-unity-android.html
but the majority of the information is old and links don't exist. I managed to rewrite the java so it would compile, but it crashes on the device, but I never see gyro data before the crash. I notice someone in that thread said they had crashing and managed to solve it by signing the jar file (I'm a total java n00b), I have no idea how to do this. I'm not even sure if I'm exporting the jar file correctly, though Unity doesn't complain.
I'd really appreciate any help anyone can offer. I need the gyro data or my game idea goes out the window.
These days Input.gyro is supported still cant figure out how to get live orientation though. Is Input.gyro.attitude.eulerAngles worth using?
Answer by Talimar · Aug 23, 2011 at 01:10 AM
Going to answer my own question here. It involves Java and the AndroidJNI/AndroidJavaClass in Unity.
http://randomactsofdev.wordpress.com
Joshua has it mostly right here, but I think his C# code is fault, in that if you try to call the constructor it will crash, as there is no constructor, and you should not instantiate classes every frame, cache them. Lastly, you need to use the generic methods for float to access the static fields (and note, you must use static fields, instance ones don't seem to work for me).
Answer by fherbst · Sep 06, 2011 at 04:07 PM
For Android, you could also use a plugin by prefrontal cortex which is available in the asset store. It provides access to all Android sensors.
Demo application (type into your mobile browser to install): goo.gl/oLkOQ
Forum link: goo.gl/8ZIM7
Thanks, but I wasn't looking for a paid plugin. I ended up getting access via java anyway. I'm suprised I forgot to update this post.
Answer by CHPedersen · Aug 17, 2011 at 09:59 AM
The Galaxy Tab has an accelerometer, not a gyroscope. Is that what you want? You can access its values through Input.acceleration, see documentation:
http://unity3d.com/support/documentation/Manual/Input.html
That should tell you how the device is oriented. Will that do for your game idea?
EDIT:
My bad about the galaxy tab; I can't confirm it doesn't have a gyroscope, so it might. It just seems Input.gyro is for Unity iOS only, and not for Android. There's a link to another question that states this here:
http://answers.unity3d.com/questions/153383/unity-34-gyroscope-support-not-there.html
Ugh, I wrote a lot and this website kicked up an error, told me to correct it and wiped everything I wrote. $$anonymous$$oving to a comment also eats posts. Awful forum software this is. Let me try this again.
As far as I am aware, the tab has all the sensors. Compasses work and the gyroscope test work, and the android sdk virtual device says hw.gyroscope: yes. Also this says it has one: http://www.gsmarena.com/samsung_p1000_galaxy_tab-3370.php
Regardless though, I may be jumping the gun. I should have done a simple java class test first, as Im now finding I can't even read a simple hardcoded value from it. For some reason I thought AndroidJavaClass would be able to interface, but even though Unity doesn't complain, I don't think it's going to be that simple (forgetting the fact I've been working on this and other related issues for 12 hours now).
I wont be able to do a C++ plugin because I can't afford Unity Pro. I'm a 100% java n00b as of today, so I'm going to have to learn some more of that and do some trials there.
$$anonymous$$y game idea requires that I can sense movement of the player, not just the orientation of the device. I believe I can do this with a combination of the accelerometer and gyro.gravity.
Oooh, I see. $$anonymous$$y bad, I updated my answer with a link to another question with more information. (I saw you participated in the other question yourself, so this is just in case somebody stumbles over this during a search)
Follow this Question
Related Questions
How should I interpret the gyro's information? 0 Answers
Input.gyro.attitude not working on newer Android devices 3 Answers
Strange android gyrometer attitude angles 1 Answer
Input.gyro.attitude returns zero values when tested with Moto G 4th generation device 2 Answers
I would like to know if with a unity free is possible to use gyroscope android? 1 Answer