Reputation: 329
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");
}
}
}
If I run the code nothing happens and I don't know why.
Upvotes: 6
Views: 28298
Reputation: 341
I would suggest the following function for the mouse scroll input:
public float GetScroll() {
return inputs.Player.Scroll.ReadValue<Vector2>().normalized.y;
}
It returns the float value 1, -1, 0.
Upvotes: 1
Reputation: 61
Using code alone you can do this:
Vector2 vec = Mouse.current.scroll.ReadValue();
scroll = vec.y
Upvotes: 4
Reputation: 809
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.
Another option that is better would be to let the value pass through and add a "Binding".
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");
Upvotes: 12