Unable to access Gyroscope in the new Input System

Hi, I have a setup as illustrated in this image:

Aim with mouse works so the problem is only in the Gyroscope access. I am enabling the Gyroscope in my InputManager with:

private void OnEnable()
    {
        inputActions.Enable();
        #if UNITY_ANDROID
            InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
        #elif UNITY_IOS
            InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
        #endif
    }

Then, I access it to my CharacterController like with this:

inputActions.PlayerInput.Aiming.performed += ctx => aimDelta = ctx.ReadValue<Vector2>();
inputActions.PlayerInput.Aiming.canceled += ctx => aimDelta = Vector2.zero;

What might be the problem here?

1 Like

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.

So you are getting performed callbacks but the parameter value is always Vector2.zero?

My understanding of gyroscope angular velocity is that it returns a Vector3. Maybe you need to do ctx.ReadValue() ?

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

2 Likes

I know you’re trying to go event-based here, but can you check if at least this is working? Tutorial for Input System and Accelerometer / Gyro?

Also what kind of device are you testing with?

1 Like

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.

1 Like

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.

3 Likes

Same thing with 1.4.4, at least from my experience. I’m unsure if this issue was ever fixed. Its kinda sad :frowning:

3 Likes

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

I enable the gyro, since all sensors are disabled by default, as said here
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Sensors.html

InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);

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…

2 Likes

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

8783746--1192867--upload_2023-2-6_0-17-4.png

3 Likes

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:

  1. 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”.

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

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

1 Like

After using the delay of some seconds, UnityEngine.InputSystem.Gyroscope.current is still null

           private UnityEngine.InputSystem.Gyroscope gyro;
 
           private IEnumerator Start()
           {

               yield return new WaitForSeconds(3f);

               gyro = UnityEngine.InputSystem.Gyroscope.current;

               if (gyro != null)
               {
                   InputSystem.EnableDevice(gyro);

...

@jfreire-unity can you comment on this thread and set the record straight?


Possibly, not all stated workflows are working correctly?

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!

//InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
InputSystem.EnableDevice(UnityEngine.InputSystem.AttitudeSensor.current);

Check my Unity Asset — Sensor Camera.

1 Like

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.

thanks it work to me

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