- Home /
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?
It very much depends on how you're controlling your camera. For instance, if you're using the mouse orbit script that comes in standard assets, you can just change the distance variable.
You should make this an answer. It's how I did my mouse-wheel. But note, it's different from changing the FOV. Similar, not same. See below.
Answer by aldonaletto · Feb 17, 2012 at 10:47 PM
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;
}
While its a simple solution, it should be carefully considered. FOV shouldn't be used to adjust the camera distance in most circumstances as this is how wide your vision is, not directly camera distance from the player. It could cause distortion in extreme cases and for some players can cause nausea at certain levels.
However if you are using an isometric camera like me, then you have to use size. It's basically the same, cause you are still controlling how much you can see.
Consider carefully if using FOW or change the distance between camera or target. It depends on what sort of zoom you want to have.
If you want a scoop like zoom you want to use the FOW. If you want to zoom in on your character you want to change the distance between target and camera.
The distortion is proportional to the FOV angle. As a rule of thumb, FOV is the best choice for zoo$$anonymous$$g in from regular view (like a sniper scope). If you want to zoom out from regular view, move back the camera - wider FOV angles create too much distortion.
Where should I add this script too, I have tried to the camera and to a map (gameobject) and they both didn't work. I get a error message saying 'NullReferenceException: Object reference not set to an instance of an object Camera$$anonymous$$anager.Update () (at Assets/Camera$$anonymous$$anager.js:9) '
Answer by Jellewho · Jul 31, 2018 at 06:34 PM
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
}
or just: Camera.main.transform.position += Camera.main.transform.forward * ScrollWheelChange;
Answer by kylebarker82 · Jul 06, 2015 at 03:53 AM
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;
That produced this error for me:
Assets/$$anonymous$$ouseWheelZoom.cs(39,1): error CS8025: Parsing error
Script file:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseWheelZoom : $$anonymous$$onoBehaviour {
float curZoomPos, zoomTo; // curZoomPos will be the value
float zoomFrom = 20f; //$$anonymous$$idway 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 = $$anonymous$$athf.Clamp (curZoomPos, 5f, 35f);
// Stops "zoomTo" value at the nearest and farthest zoom value you desire
zoomTo = $$anonymous$$athf.Clamp (zoomTo, -15f, 30f);
// $$anonymous$$akes the actual change to Field Of View
Camera.main.fieldOfView = curZoomPos;
}
You need another } at the end of your script.
Answer by astracat111 · Nov 07, 2016 at 07:42 AM
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;
Answer by MiniGeek-Youtube · Jun 05, 2021 at 12:12 PM
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