0
votes

I'm currently trying to detect the mouse wheel scrolling input for my 2D Game I am currently doing in the Unity Engine.

I'm using the new Input System and I'm currently stuck with the follwing code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Controls;

public class ZoomController : MonoBehaviour
{

private PlayerControls playerControls;

private void Awake()
{
    playerControls = new PlayerControls();
}


private void OnEnable()
{
    playerControls.Enable();
}

private void OnDisable()
{
    playerControls.Disable();
}

void Start()
{
    
}

void Update()
{

    if (playerControls.Land.Zoom.ReadValue<Vector2>().y >= 1)
    {
        Debug.Log("Scroll 1");
    } else if (playerControls.Land.Zoom.ReadValue<Vector2>().y <= -1)
    {
        Debug.Log("Scroll 2");
    }
    
 }
}

enter image description here

If I run the code nothing happens and I don't know why.

2

2 Answers

2
votes

If you treat the scroll wheel as a "1D Axis" you need to tell Unity which side (up or down) should win. Otherwise, you will only receive an average value which will always be 0.

Action as 1D Axis - tell Unity which side wins

Another option that is better would be to let the value pass through and add a "Binding". enter image description here

enter image description here

Note that the default scroll value is 120 on most systems, but on Linux it seems to be 1. I would therefore recommend simply checking if the value is larger or smaller than 0 to decide if it is scrolling up or down.

float z = zoom.ReadValue<float>();
if (z > 0)
    Debug.Log("Scroll UP");
else if (z < 0)
    Debug.Log("Scroll DOWN");
1
votes

Using code alone you can do this:


    Vector2 vec = Mouse.current.scroll.ReadValue();
    scroll = vec.y