Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unable to access Gyroscope in the new Input System

Discussion in 'Input System' started by Virtumonde, Jan 7, 2021.

  1. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    Hi, I have a setup as illustrated in this image: Screen Shot 2021-01-07 at 11.47.09 PM.png Aim with mouse works so the problem is only in the Gyroscope access. I am enabling the Gyroscope in my InputManager with:
    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         inputActions.Enable();
    4.         #if UNITY_ANDROID
    5.             InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
    6.         #elif UNITY_IOS
    7.             InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
    8.         #endif
    9.     }
    Then, I access it to my CharacterController like with this:
    Code (CSharp):
    1. inputActions.PlayerInput.Aiming.performed += ctx => aimDelta = ctx.ReadValue<Vector2>();
    2. inputActions.PlayerInput.Aiming.canceled += ctx => aimDelta = Vector2.zero;
    What might be the problem here?
     
    makaka-org likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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?
     
  3. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    @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.
     
    Last edited: Jan 8, 2021
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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<Vector3>() ?
     
  5. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    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>()
    ).
     
    makaka-org and MilenaRocha like this.
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Virtumonde likes this.
  7. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    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.
     
    PraetorBlue likes this.
  8. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    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.
     
    Last edited: Jan 8, 2021
  9. AndSet

    AndSet

    Joined:
    Nov 17, 2020
    Posts:
    1
    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.
     
  10. TheBen67

    TheBen67

    Joined:
    Dec 28, 2022
    Posts:
    3
    Same thing with 1.4.4, at least from my experience. I'm unsure if this issue was ever fixed. Its kinda sad :(
     
    makaka-org, MilenaRocha and ATate like this.
  11. MilenaRocha

    MilenaRocha

    Joined:
    Jun 30, 2018
    Posts:
    12
    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
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. 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:
    <Gyroscope>/angularVelocity​
    Supposedly can get the axis individually by adding a "/{axis}"
    <Gyroscope>/angularVelocity/x​
    And it doesn't work, since the values never change

    Also tried manually scripting it
    Code (CSharp):
    1. if (gyroscope != null)
    2.     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...
     
    Last edited: Mar 7, 2023
    makaka-org and MaxBackcountry like this.
  12. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using Gyroscope = UnityEngine.InputSystem.Gyroscope;
    4.  
    5. public class GyroComponent : MonoBehaviour
    6. {
    7.     private void Update()
    8.     {
    9.         if (!SystemInfo.supportsGyroscope) return;
    10.         if (Gyroscope.current != null)
    11.         {
    12.             if (Gyroscope.current.enabled)
    13.             {
    14.                 Debug.Log($"InputSystem Gyro: {Gyroscope.current.angularVelocity.ReadValue()}");
    15.             }
    16.             else
    17.             {
    18.                 Debug.Log("Gyroscope not enabled. Enabling now.");
    19.                 InputSystem.EnableDevice(Gyroscope.current);
    20.             }
    21.         }
    22.     }
    23. }
    24.  
    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()
    .

    upload_2023-2-6_0-17-4.png
     
    Last edited: Feb 5, 2023
    TheyCallMeTJ and MaxBackcountry like this.
  13. MaxBackcountry

    MaxBackcountry

    Joined:
    Jan 24, 2014
    Posts:
    9
    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".


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


    3. 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.
     
    TheyCallMeTJ likes this.
  14. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    872
    After using the delay of some seconds, UnityEngine.InputSystem.Gyroscope.current is still null

    Code (CSharp):
    1.            private UnityEngine.InputSystem.Gyroscope gyro;
    2.  
    3.            private IEnumerator Start()
    4.            {
    5.  
    6.                yield return new WaitForSeconds(3f);
    7.  
    8.                gyro = UnityEngine.InputSystem.Gyroscope.current;
    9.  
    10.                if (gyro != null)
    11.                {
    12.                    InputSystem.EnableDevice(gyro);
    13.  
    14. ...
    @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:

    Code (CSharp):
    1. cameraMain.localRotation = gyro.attitude;
    2.  
    3. to
    4.  
    5. 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!

    Code (CSharp):
    1. //InputSystem.EnableDevice(UnityEngine.InputSystem.Gyroscope.current);
    2. InputSystem.EnableDevice(UnityEngine.InputSystem.AttitudeSensor.current);
     
    Last edited: Jun 11, 2023