Mouse Wheel Zoom

Hey all, I have a ortho camera, I can't figure out how to make it so the mouse wheel will scroll in or out. I need the script to change main.camera.orthographicSize in increments of 1. Max 6 Min 1.

If you provide a script can you give me a brief explanation of how it works, specifically how I code for zoom in vs zoom out?

Thanks! Joe

function Update () {
    if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
        {
            Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize-1, 1);

        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
        {
            Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize-1, 6);
        }
}

function Update () 
{
    const int orthographicSizeMin = 1;
    const int orthographicSizeMax = 6;

    if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
    {
        Camera.main.orthographicSize++;
    }
    if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
    {
        Camera.main.orthographicSize--;
    }

    Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, orthographicSizeMin, orthographicSizeMax );
}

Comprehensive code, fully usable without editing:

public class ExampleClass : MonoBehaviour {
	public float scrollDistanceIncrement;

	// Optional |: public float maxScroll;

	void Update () {
		
		// Get mouse scrollwheel forwards || check if main camera is too small, prevents a couple errors
		if (Input.GetAxis ("Mouse ScrollWheel") > 0 && Camera.main.orthographicSize > 1) {
			// Scroll camera inwards
			Camera.main.orthographicSize = Camera.main.orthographicSize -= scrollDistanceIncrement;//Mathf.Max (Camera.main.orthographicSize - 1, 1);
		}

		// Get mouse scrollwheel backwards || optional code
		if (Input.GetAxis ("Mouse ScrollWheel") < 0 /*Optional |: && Camera.main.orthographicSize < maxScroll :| */) {
			// Scrolling Backwards
			Camera.main.orthographicSize = Camera.main.orthographicSize += scrollDistanceIncrement;//Mathf.Min (Camera.main.orthographicSize - 1, 1);
		}

	}

	/* Legend:
	 * |: <code> :| is code
	 * || are just seperators
	 */
}

Upvote if this was helpful.

you can use this expression to get back -1, 0, or 1.

(Input.GetAxis(“MouseWheel”) != 0 ? Mathf.Sign(Input.GetAxis(“MouseWheel”)) : 0)

and then just do it all with one line like,

Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + (Input.GetAxis("MouseWheel) != 0 ? Mathf.Sign(Input.GetAxis(“MouseWheel”)) : 0), zoomMin, zoomMax);

i try to do some code like this:

     if (Input.GetAxis("Mouse ScrollWheel") > 0)
                {
                    camera.orthographicSize--;
                }
    
                if (Input.GetAxis("Mouse ScrollWheel") < 0)
                {
                    camera.orthographicSize++;
                }

But unity act on this only when i am holding CTRL.

I used some of the code in this thread and wrote my own using c#. When you run the script it will set the camera to the current cameraCurrentZoom, then recognise when the player uses the scroll wheel and manipulate the current zoom level to that number. This is, of course, designed for a 2D Orthographic camera.

using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour {

public int cameraCurrentZoom = 8;
public int cameraZoomMax = 20;
public int cameraZoomMin = 5;

void Start ()
{
    Camera.main.orthographicSize = cameraCurrentZoom;
}

void Update () {

    if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
    {
        if (cameraCurrentZoom < cameraZoomMax)
        {
            cameraCurrentZoom += 1;
            Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize + 1);
        } 
    }

    if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
    {
        if (cameraCurrentZoom > cameraZoomMin)
        {
            cameraCurrentZoom -= 1;
            Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize - 1);
        }   
    }

    }
}

bad examples