how to scroll zoom camera unity code example
Example 1: camera zooming in unity
using UnityEngine;
using System.Collections;
public class MouseWheelZoom : MonoBehaviour {
float curZoomPos, zoomTo;
float zoomFrom = 20f;
void Update ()
{
float y = Input.mouseScrollDelta.y;
if (y >= 1)
{
zoomTo -= 5f;
Debug.Log ("Zoomed In");
}
else if (y >= -1) {
zoomTo += 5f;
Debug.Log ("Zoomed Out");
}
curZoomPos = zoomFrom + zoomTo;
curZoomPos = Mathf.Clamp (curZoomPos, 5f, 35f);
zoomTo = Mathf.Clamp (zoomTo, -15f, 30f);
Camera.main.fieldOfView = curZoomPos;
}
Example 2: unity scroll zoom
int minZoom = 40;
int maxZoom = 70;
int zoomValue = 10;
Camera _cam;
void Awake() => _cam = Camera.main;
if (Input.mouseScrollDelta.y > 0)
{
_cam.fieldOfView = Mathf.Clamp(_cam.fieldOfView -= zoomValue, minZoom, maxZoom);
}
else if (Input.mouseScrollDelta.y < 0)
{
_cam.fieldOfView = Mathf.Clamp(_cam.fieldOfView += zoomValue, minZoom, maxZoom);
}