Can you expand a bit more on what you mean by “unable to access” the gyroscope? What’s happening? Compile error? Runtime error? Not getting callbacks? Invalid data in the callbacks?
@PraetorBlue Essentially the data is invalid, there is no movement registered at all (the Vector 2 is zero at all axes) even though the phone is being rotated. I don’t gent any errors.
Still no luck. Checked if the Gyroscope is activated and UnityEngine.InputSystem.Gyroscope.current.enabled) returns true. I also used the whole Gyroscope binding and still the Vector3 is zero at every axis (I specifically used ctx.ReadValue<UnityEngine.Vector3>()).
Hmm, using values directly works perfectly. I wonder why it is not working the other way. I am using a normal Android device. Does the model matter? I was able to get gyroscope data using the old input system.
Okay so the new Input System is as bad as its reputation. I guess I’ll wait till they will make a fix for this and till that I am switching back to the old system. I have more problems that solutions with this one. Besides this, I have a couple of inexplicable things going on with buttons so I think it will be better just to switch back. I will report this as a bug in the meantime.
I confirm, it’s still not fixed in the new Input System. It’s impossible to access Gyroscope data from AndroidGyroscope.current. I am using the New Input System 1.0.2 v.
Same bug here - using Input System v 1.3.0
Trying it with the Unity Remote app - iOS device
The Remote device takes some time to connect, and it sends a callback when it does
Unity Remote connected to input!
UnityEditor.Remote.GenericRemote:CallMessageHandlers
Only when the Remote connects I do the following:
I check if there is an available gyro
Debug.Log("Is there a gyroscope currently? " + UnityEngine.InputSystem.Gyroscope.current != null);
And then the gyro angularVelocity always returns a Vector3.zero (0.00, 0.00, 0.00) Tried on the InputAction asset
Path for the gyroscope (Vector3) is:
/angularVelocity
Supposedly can get the axis individually by adding a “/{axis}”
/angularVelocity/x
And it doesn’t work, since the values never change
Also tried manually scripting it
if (gyroscope != null)
Debug.Log("gyroscope.angularVelocity is " + UnityEngine.InputSystem.Gyroscope.current.angularVelocity.ReadValue());
returns
gyroscope.angularVelocity is (0.00, 0.00, 0.00)
Again, not working
Going back to the old input system for now until this gets fixed…
I never really used Unity Remote because of all its quirks. I never felt it is a realistic test in any way.
Though I’ve connected my iPhone 13 and used this
using UnityEngine;
using UnityEngine.InputSystem;
using Gyroscope = UnityEngine.InputSystem.Gyroscope;
public class GyroComponent : MonoBehaviour
{
private void Update()
{
if (!SystemInfo.supportsGyroscope) return;
if (Gyroscope.current != null)
{
if (Gyroscope.current.enabled)
{
Debug.Log($"InputSystem Gyro: {Gyroscope.current.angularVelocity.ReadValue()}");
}
else
{
Debug.Log("Gyroscope not enabled. Enabling now.");
InputSystem.EnableDevice(Gyroscope.current);
}
}
}
}
It does work (Input System 1.4.4)
But things I did notice whilst debugging is that:
The Gyroscope.current is null due to the remote not started yet. So you can’t do this in Start() or OnEnable() at the launch of your game.
The Gyroscope.current.enabled is true after setting InputSystem.EnableDevice(Gyroscope.current) but later is back to false again. Which in this script I force it back to enabled.
So it can work with the new input system but you’d have to force it. Whereas with the old Input System you don’t.
You can enable the gyro in Start().
Not same, but similar problems here (Editor 2022.2.6f; Input System 1.5.0; Unity Remote 5; Android Device for testing).
Tried debugging this for hours and got it running - strange enough - every second time I enter PlayMode. Here is what I can contribute:
I think MilenaRocha is right:
You have to wait for Unity Remote to connect, otherwise calling InputSystem.EnableDevice(Gyroscope) throws an exception. So don’t call it “too early”.
I Think MaskedMouse is also right and the Gyroscope falls back to disabled at a certain point.
I use MaskedMouse snippet to “force” the Gyroscope to be enabled. But very strange, one time the Gyroscope is really enabled after that and I get the full readings from it, The next time that I enter in PlayMode, it is not. And the time after that, it works again.
No kidding, I tried at least 20 times and it is exactly 1 out of 2 tries.
Yet another strange thing: I’m trying to migrate an older project to the new Input System. The “old” script attached to my permanent game object contained the line:
Input.gyro.enabled = true;
When I comment this line out, calling EnableDevice(Gyroscope) later does not work at all.
P.S. After creating the Input Settings in the Player Settings, gyro is NOT NULL. However, this was not obvious because I didn’t change the default settings - is it a bug?
v1.6.1 and Unity 2022.3.1
P.S.2. I enabled the gyro, but AttitudeSensor.current.attitude.ReadValue(); return 0s. So no I can’t replace the old system quickly:
cameraMain.localRotation = gyro.attitude;
to
cameraMain.localRotation = AttitudeSensor.current.attitude.ReadValue();
Gyroscope.current.angularVelocity.ReadValue() is not suitable
P.S.3. Solved: Instead of Enabling Gyro, you need to enable the Attitude sensor!
I had done everything rhight and I still didn’t get attitude readings on unity remote. Then I realized it was because I needed to tap on my touch screen so that the controls would switch from the computer to the phone. Simply moving the phone didn’t do anything, but after tapping the screen, I got attitude readings.
This worked like a charm. Ill leave some keyword here for others.
Unity Gyroscope not working
Unity new Input System
Unity Gyroscope attitude returning 0, 0, 0
Gyroscope attitude not working
How to use Unity Gyroscope
Unity Gyroscope returning zeroes