How do I make the camera zoom in and out with the mouse wheel?

Does anyone know how I can make my camera zoom in and out based on the mouse wheel being scrolled?

The easiest way to zoom in and out is to change the field of view - narrowing the angle zooms in, widening zooms out):

var minFov: float = 15f;
var maxFov: float = 90f;
var sensitivity: float = 10f;

function Update () {
  var fov: float = Camera.main.fieldOfView;
  fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
  fov = Mathf.Clamp(fov, minFov, maxFov);
  Camera.main.fieldOfView = fov;
}

This little piece of code DOES NOT USE FOV, but moved the camera back and forward, as it should be.
This frees up your FOV settings again and such.
It cost me a few hours to get those damn angles right, but it converts the angle of the main camera to a location and moves the camera than a certain radius

float ScrollWheelChange = Input.GetAxis("Mouse ScrollWheel");           //This little peece of code is written by JelleWho https://github.com/jellewie
        if (ScrollWheelChange != 0){                                            //If the scrollwheel has changed
            float R = ScrollWheelChange * 15;                                   //The radius from current camera
            float PosX = Camera.main.transform.eulerAngles.x + 90;              //Get up and down
            float PosY = -1 * (Camera.main.transform.eulerAngles.y - 90);       //Get left to right
            PosX = PosX / 180 * Mathf.PI;                                       //Convert from degrees to radians
            PosY = PosY / 180 * Mathf.PI;                                       //^
            float X = R * Mathf.Sin(PosX) * Mathf.Cos(PosY);                    //Calculate new coords
            float Z = R * Mathf.Sin(PosX) * Mathf.Sin(PosY);                    //^
            float Y = R * Mathf.Cos(PosX);                                      //^
            float CamX = Camera.main.transform.position.x;                      //Get current camera postition for the offset
            float CamY = Camera.main.transform.position.y;                      //^
            float CamZ = Camera.main.transform.position.z;                      //^
            Camera.main.transform.position = new Vector3(CamX + X, CamY + Y, CamZ + Z);//Move the main camera
        }

You can use your own values to change field of view, but this script works just for the scroll wheel itself. I have this script attached to the camera in the scene.

float curZoomPos, zoomTo; // curZoomPos will be the value
	float zoomFrom = 20f; //Midway point between nearest and farthest zoom values (a "starting position")
	

	void Update ()
	{
		// Attaches the float y to scrollwheel up or down
		float y = Input.mouseScrollDelta.y;

		// If the wheel goes up it, decrement 5 from "zoomTo"
		if (y >= 1)
		{
			zoomTo -= 5f;
			Debug.Log ("Zoomed In");
		}

		// If the wheel goes down, increment 5 to "zoomTo"
		else if (y >= -1) {
			zoomTo += 5f;
			Debug.Log ("Zoomed Out");
		}

		// creates a value to raise and lower the camera's field of view
		curZoomPos =  zoomFrom + zoomTo;

		curZoomPos = Mathf.Clamp (curZoomPos, 5f, 35f);

		// Stops "zoomTo" value at the nearest and farthest zoom value you desire
		zoomTo = Mathf.Clamp (zoomTo, -15f, 30f);

		// Makes the actual change to Field Of View
		Camera.main.fieldOfView = curZoomPos;

Putting this in the FreeLookCam.cs script’s Update() or LateUpdate() will allow you to zoom in and out using Unity3D’s standard free look prefab camera controller:

float mouseScrollSpeed = 5f;
m_OriginalDist -= Input.GetAxisRaw("Mouse ScrollWheel") * mouseScrollSpeed;

The code in C#:

    public float minFOV;
    public float maxFOV;
    public float sensitivity;
    public float FOV;

    void Update()
    {
        FOV = Camera.main.fieldOfView;
        FOV += (Input.GetAxis("Mouse ScrollWheel") * sensitivity) * -1;
        FOV= Mathf.Clamp(FOV, minFOV, maxFOV);
        Camera.main.fieldOfView = FOV;
    }

I thought that the scrolling should be reversed (scrolling down zooms out), but if you think otherwise, just remove the * -1
Hope this helped

float camZoom = -10f;
float camZoomSpeed = 2f;
Transform Cam;

void Start (){
Cam = this.transform;
}

void Update (){
camZoom += Input.GetAxis("Mouse ScrollWheel") * camZoomSpeed;
transform.position = new Vector3(Cam.position.x, Cam.position.y, camZoom );   // use localPosition if parented to another GameObject.
}

This is “clamp” version of @jellewho’s script, credits should go to him, I am sharing if anyone needs this version.
This one works when Camera is child. It functions with camera zoom in and out for certain distances (min/max) you define by two public variables.

private float scrollWheelChange;
private Camera cam;
private float R;
private float PosX;
private float PosY;
private float X;
private float Y;
private float Z;
private float CamX;
private float CamY;
private float CamZ;
private float CamZlocal;
public float zoomSpeed = 3;
public float clampHeightMax = 30f;
public float clampHeightMin = -125f;

void Start () {
	cam = GetComponent<Camera>();
}

void Update () {
	scrollWheelChange = Input.GetAxis("Mouse ScrollWheel");           //This little peece of code is written by JelleWho https://github.com/jellewie
	if (scrollWheelChange != 0){                                            //If the scrollwheel has changed
		R = scrollWheelChange * 15 * zoomSpeed;                                   //The radius from current camera
		PosX = cam.transform.eulerAngles.x + 90;              //Get up and down
		PosY = -1 * (cam.transform.eulerAngles.y - 90);       //Get left to right
		PosX = PosX / 180 * Mathf.PI;                                       //Convert from degrees to radians
		PosY = PosY / 180 * Mathf.PI;                                       //^
		X = R * Mathf.Sin(PosX) * Mathf.Cos(PosY);                    //Calculate new coords
		Z = R * Mathf.Sin(PosX) * Mathf.Sin(PosY);                    //^
		Y = R * Mathf.Cos(PosX);                                      //^
		CamX = cam.transform.position.x;                      //Get current camera postition for the offset
		CamY = cam.transform.position.y;                      //^
		CamZ = cam.transform.position.z;                      //^
		CamZlocal = cam.transform.localPosition.z; 
		if (CamZlocal + Z >= clampHeightMin && CamZlocal + Z <= clampHeightMax){
		cam.transform.position = new Vector3(CamX + X, CamY + Y, CamZ + Z);//Move the main camera
		}
	}
}

Here is my code, simplified from some of the answers here.

    public class CameraZoomController : MonoBehaviour
    {
        private Camera Cam;
        public float CamSize;
        public float zoomDelta;
        float minZoom = 10f;
        float maxZoom = 1000f;
    
        void Awake()
        {
            // find camera and starting size
            Cam = Camera.main;
            CamSize = Cam.orthographicSize;
        }
        void Update()
        {
            // get the current size + the scrollwheel change
            zoomDelta = CamSize + Input.mouseScrollDelta.y;
            // constrain zoom
            CamSize = Mathf.Clamp(zoomDelta, minZoom, maxZoom);
            // apply zoom to field of view
            Cam.orthographicSize = CamSize;
        }
    }

Why are people still answering this? :smiling_face:

using UnityEngine;

public class CameraController : MonoBehaviour
{
    public Vector3 offset = new Vector3(0,0,1);
    public Transform target;

    public float minZoom = 5f;
    public float maxZoom = 15f;
    public float zoomSpeed = 4f;

    private float currentZoom = 10f;

    void Update ()
    {
        currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
        currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
    }

    void LateUpdate ()
    {
        transform.position = target.position - offset * currentZoom;
    }
}

private void CameraZoom()
{
float zoomValue = Input.GetAxis(“Mouse ScrollWheel”) * cameraZoomSensitivity;
//mainCamera is just the Transform of the Camera.
mainCamera.Translate(new Vector3(0.0f, 0.0f, zoomValue));
}

Orthographic version, which changes Projection size:

    [SerializeField] float minSize = 4f;
    [SerializeField] float maxSize = 20f;
    [SerializeField] float sensitivity = 10f;

    Camera cam;

    // Start is called before the first frame update
    void Start()
    {
        cam = GetComponent<Camera>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            float size = cam.orthographicSize;
            size += Input.GetAxis("Mouse ScrollWheel") * sensitivity * Time.deltaTime;
            size = Mathf.Clamp(size, minSize, maxSize);
            cam.orthographicSize = size;
        }

    }