InputSystem icon indicating copy to clipboard operation
InputSystem copied to clipboard

Unwanted Deadzone when calling `gamepad.stick.x.ReadUnprocessedValue()`

Open garettbass opened this issue 8 years ago • 5 comments

Unity 2018.3.0b8, Windows 10

I'm testing with both DualShock 4 and Xbox 360 Controller for Windows, drawing the stick output in an editor window during play mode. I'm retrieving the stick X and Y values as follows:

var stickX = stick.x.ReadUnprocessedValue();
var stickY = stick.y.ReadUnprocessedValue();

I draw the stick position as a dot inside a circle using Handles.DrawSolidDisc() (complete listing below).

I can easily trace a smooth circle with either stick using the DualShock 4 controller, but the Xbox 360 controller sticks near the North/South/East/West points on the circle. It looks to me like deadzoning of ~0.2f is being applied to each axis independently, even when calling ReadUnprocessedValue(). It does not appear possible to get unprocessed values near zero from either axis of either stick when using the Xbox 360 controller.

using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.Input;
using UnityEngine.Experimental.Input.Controls;

public class InputMonitor : EditorWindow
{

    [MenuItem("Window/Input Monitor")]
    public static InputMonitor Open()
    {
        var window = GetWindow<InputMonitor>(
            title: "Input Monitor",
            desiredDockNextTo: typeof(SceneView));
        return window;
    }

    private void Update()
    {
        Repaint();
    }

    private Vector2 m_scrollOffset;

    private void OnGUI()
    {
        GUILayoutUtility.GetRect(0, 2);
        InputConfiguration.DeadzoneMin = 0f;
        InputConfiguration.DeadzoneMax = 1f;
        m_scrollOffset = GUILayout.BeginScrollView(m_scrollOffset);
        foreach (var gamepad in Gamepad.all)
        {
            DrawGamepadGUI(gamepad);
        }
        GUILayout.EndScrollView();
    }

    private void DrawGamepadGUI(Gamepad gamepad)
    {
        EditorGUILayout.LabelField(gamepad.name, EditorStyles.boldLabel);
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        DrawGamepadStickGUI(gamepad.leftStick);
        GUILayout.Space(32);
        DrawGamepadStickGUI(gamepad.rightStick);
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
    }

    private void DrawGamepadStickGUI(StickControl stick)
    {
        var playing = Application.isPlaying;
        var radius = 32;
        var diameter = radius * 2;
        var rect = GUILayoutUtility.GetRect(diameter, diameter);
        var center = rect.center;
        Handles.color = Color.Lerp(Color.grey, Color.clear, 0.25f);
        Handles.DrawSolidDisc(center, Vector3.forward, radius);
        Handles.color = Color.white;
        var stickX = playing ? +stick.x.ReadUnprocessedValue() : 0;
        var stickY = playing ? -stick.y.ReadUnprocessedValue() : 0;
        var stickVector = new Vector2(stickX, stickY);
        var stickMagnitude = Mathf.Min(stickVector.magnitude, 1f);
        var stickDirection = stickVector.normalized;
        var stickDisplacement = stickDirection * stickMagnitude * radius;
        var stickCenter = (center + stickDisplacement);
        Handles.DrawSolidDisc(stickCenter, Vector3.forward, radius / 4);
    }

}

garettbass avatar Nov 07 '18 06:11 garettbass

When querying x or y directly, deadzoning currently doesn't apply at all actually (i.e. leftStick.ReadValue().x is deadzoned but leftStick.x.ReadValue() isn't) -- which is an unfortunate effect of how processors work and probably needs tweaking. Meaning that the only processing you're getting is actually the clamping/inverting/etc done internally by AxisControl.

So the problem here is probably coming from something else. Could be there's something we can tweak here. Something to look into.

Rene-Damm avatar Nov 08 '18 19:11 Rene-Damm

Hi Rene, thank you for taking a moment to review this. I believe there may be some misunderstanding.

When querying x or y directly, deadzoning currently doesn't apply at all...

My problem is that deadzoning DOES APPLY to the Xbox 360 Controller stick axes, even though I am trying to ReadUnprocessedValue(), where I expect to get the raw value.

You will also notice in the Input Debugger window, that it is possible to move any of the Xbox 360 Controller stick axes about 20% before they register any displacement at all.

My suspicion is that the deadzone is being applied per axis on the native side, somewhere near where the XInput API XInputGetState() is called, and that the C# side of the InputSystem can't ever receive the raw stick axis values. If this is the case, will we need to wait for a new build of Unity to fix this issue?

garettbass avatar Nov 08 '18 19:11 garettbass

You will also notice in the Input Debugger window, that it is possible to move any of the Xbox 360 Controller stick axes about 20% before they register any displacement at all.

Ok, that is very curious and definitely needs looking at. As far as our code is concerned, there's no deadzoning going on there. We're literally sending XINPUT_GAMEPAD as we receive it from the XInput DLL.

        virtual void Poll()
        {
            XINPUT_STATE state;
            if (s_XInputGetStateFn(userIndex, &state) != ERROR_SUCCESS)
                return;

            // Ignore if controller has not changed.
            if (lastPacketNumber == state.dwPacketNumber)
                return;

            // Send event.
            StateInputEventData<XINPUT_GAMEPAD> event;
            event.type = kInputEventState;
            event.deviceId = deviceId;
            event.time = GetInputEventTimeNow();
            event.sizeInBytes = sizeof(event);
            event.stateFormat = 'XINP';
            memcpy(&event.stateData, &state.Gamepad, sizeof(XINPUT_GAMEPAD));
            QueueInputEvent(event);

            lastPacketNumber = state.dwPacketNumber;
        }

So that makes me think this has to be coming from somewhere else that sits before we even get the data. Wondering if it's the Windows device calibration that's doing that.

Rene-Damm avatar Nov 08 '18 19:11 Rene-Damm

Thanks for a peek at the native side. I will investigate the managed side further over the weekend. Cheers!

garettbass avatar Nov 08 '18 20:11 garettbass

Resolved as by design. https://issuetracker.unity3d.com/issues/controller-unwanted-deadzone-when-calling-gamepad-dot-stick-dot-x-readunprocessedvalue

JiaUnity avatar Mar 27 '19 21:03 JiaUnity