Unity InputSystem Scroll [Mouse] is triggered by middle mouse button press. Possible bug?

83 Views Asked by At

I'm using the Unity InputSystem to detect mouse scrolling and use it's Callback to effect a Camera object zoom. However, I noticed that when I press (not scroll) on the middle mouse button (aka the scroll wheel), it registers the same as a forward mouse Scroll. There are no custom interactions or additional Input Actions. Is this a bug in Unity's InputSystem or a "feature" that can be disabled? Because I don't want it.

Reproduce by setting up an InputActions called "Controls" with an Action Map called "InputControl" and an Action called "Zoom". The Zoom action should have an Action type of Value and Control type of Vector 2. Add a binding with a Path of Scroll [Mouse]. (Note: There are no custom Interactions or Processors that could be interfering.) Save the Input Actions and generate the C# Class.

Create an empty Game Object (I called mine Camera Rig), and child a Camera to it. Give the Camera a position value of 0, 10, -10 (or similar) and a rotation value of 30, 0, 0 (or similar).

Create a C# script called Zoom with the below code and attach it to the Camera Rig game object. You should find that you can use the mouse scroll wheel to zoom the camera in/out toward the Camera's parent (Camera Rig).

HOWEVER, pressing the middle mouse button ALSO triggers the Zoom action as if you'd scrolled forward. There doesn't seem to be any reason why this should be happening, and I don't like it. Am I missing something?

using UnityEngine;
using UnityEngine.InputSystem;

public class Zoom : MonoBehaviour
{
    [SerializeField] float stepSize = 2f;
    [SerializeField] float minZoomDistance = 5f;
    [SerializeField] float maxZoomDistance = 30f;

    Controls zoomAction;
    Transform cameraTransform;

    void Awake()
    {
        zoomAction = new Controls();
        cameraTransform = GetComponentInChildren<Camera>().transform;
    }

    private void OnEnable()
    {
        // Subscribe to the input action
        zoomAction.InputControl.Zoom.performed += OnZoom;
        zoomAction.Enable();
    }


    private void OnDisable()
    {
        // Unsubscribe from the input action
        zoomAction.InputControl.Zoom.performed -= OnZoom;
        zoomAction.Disable();
    }

    private void OnZoom(InputAction.CallbackContext inputValue)
    {
        Vector3 direction = cameraTransform.position - transform.position;          // Calculate the vector between the objects
        float distance = direction.magnitude;                                       // Calculate the current distance
        float value = Mathf.Sign(inputValue.ReadValue<Vector2>().y);                // normalize inputValue .y to 1 or -1

        if (distance < minZoomDistance && value > 0f)
        {
            return;
        }
        if ( distance > maxZoomDistance && value < 0f)
        {
            return;
        }

        direction.Normalize();
        cameraTransform.position = Vector3.MoveTowards(cameraTransform.position, transform.position, value * stepSize);
        

     }
}
1

There are 1 best solutions below

0
Jon Simon On

Found solution.

There was a configuration issue in my mouse (Logitech G500s) that was somehow causing this problem. It was NOT, in fact, a Unity bug or a coding error.

Downloading and installing the Logitech Gaming Software resolved the problem.

(Side note: Kudos to the Unity Bug Reporting team for responding so quickly after I made a report about this. The tech that responded was not able to reproduce the issue, and that caused me to rethink what might be happening. I tried with a different mouse and found that with a different mouse, the problem was not occurring.)