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

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.

Going to answer my own question here. It involves Java and the AndroidJNI/AndroidJavaClass in Unity.

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).

The Galaxy Tab has an accelerometer, not a gyroscope. Is that what you want? You can access its values through Input.acceleration, see documentation:

That should tell you how the device is oriented. Will that do for your game idea?


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:

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):

Forum link: