Mouse scroll up or down?

Hi all,

Im trying to make a map that will “zoom” in and out on the mouse scroll but have run into a issue that I cant find out how to do on the internet or on the unity documention, How do you work out if the mouse has wheel has gone up or down, have tryed a few tests but to no avail, due to the fact that im probley doing it wrong. My code is shown below:

using UnityEngine;
using System.Collections;

public class MapScroll : MonoBehaviour {

	public Camera minimap ;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetAxis("Mouse ScrollWheel") ) // forward
		{
			minimap.orthographicSize++;
		}
		if (Input.GetAxis("Mouse ScrollWheel") ) // backwards
		{
			minimap.orthographicSize--;
		}
	}
}
1 Like

Input.GetAxis( “YourAxisStringHere” )

returns a float number. To convert this into a logical bool, simply compare it to zero. Your code above should look like this.

if (Input.GetAxis("Mouse ScrollWheel") > 0f ) // forward
{
   minimap.orthographicSize++;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f ) // backwards
{
   minimap.orthographicSize--;
}

or even more simpšlfied:

if (Input.GetAxis("Mouse ScrollWheel") != 0f ) // forward
{
   minimap.orthographicSize += Input.GetAxis("Mouse ScrollWheel");
}

Each movement will register as 0.1 or -0.1.

If you scroll faster the value will increase (I can get to 0.3 / -0.3 maximum).

Note that GetAxis does not check against the actual hardware but refers to the Input Manager for the setup of the Virtual Axis. The figures I achieve are from the default setup of “Mouse ScrollWheel” entry.