Bad values for joystick input in unity 5

I have a “noname” USB joystick and I try to use it in a Unity project.
The buttons are working fine, but I have troubles with axis values.

According to Unity5 documention, for each axis, I should get values :

  • -1.0 when the axis is pushed to the “minimum” (left for X axis or down for Y axis)
  • 0.0 when the axis is at center
  • +1.0 when the axis is pushed to the “maximum” (right for X axis or up for Y axis)

But I get :

  • -1.0 when the axis is at center
  • 0.0 when the axis is halfway between center and maximum
  • +1.0 when the axis is pushed to the “maximum” (right for X axis or up for Y
  • +1.0 if the axis is pushed at any position below center

I really don’t understand why I get these values :

  • when I display the windows joystick calibration UI (run “joy.cpl”) the joystick is working fine.
  • when I use an existent game, the joystick is working fine
  • I tried some joystick test programs found on the internet, each time the joystick is working fine
  • I tried some HTML5 joystick test sites, each time the joystick is working fine

But I can’t believe there is such a problem in Unity5, so it must be in my code…

Here is a simple test code I use in Unity to display values :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class JoystickTest : MonoBehaviour
    {
        public float Position            = 0.0f;

        void Update()
        {
            float value = Input.GetAxis("Player 1");
            Position = value;
        }
    }

And the Unity input manager is configured as follow :

I’ve tried to play with input manager settings, with windows joystick settings, with my code… no way, the result is still the same : work in any application except in my Unity test project…

On Google, I’ve found some post about the same problem, but no solution…

Any help will be really appreciated !

Thanks

I don’t know. I got 0 for mid, -1 for left, and 1 for right, with your code. I renamed the joystick from Horizontal to Player 1.

I’m stumped too. I’ve worked with joysticks and gamepads a fair amount in Unity, but I’ve never seen anything like what you describe.

I believe this is due to the code interfacing with Windows Raw Input handling min and max values incorrectly. Windows reports min and max values as LONG values. However, on the HID they are actually SHORTs. Also, they are unsigned only if both the min and max as SHORTs are >=0. Otherwise they are signed. However, Windows will always perform a sign extension on them.

Someone came across this in the Windows code and added code to always mask with 0xFFFF. That’s incorrect, however, and, depending on the min/max limits of your device, will lead to the problem reported here.

Unfortunately, it’s not possible to revert this the on the user side of the API. The data is simply lost by the transformation applied with the incorrect min and max values.

So… is this officially reported (and hopefully scheduled for a fix)?

Most joysticks that exhibit this problem in Unity will work in Rewired using its custom Raw Input implementation. You can also switch to Direct Input in Rewired which will also correctly read these axis value.