Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by ProfessorBrickkeeper · Feb 17, 2012 at 10:01 PM · cameramousezoomscroll

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harabeck · Feb 17, 2012 at 11:09 PM 0
Share

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.

avatar image DaveA · Feb 18, 2012 at 12:46 AM 0
Share

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.

10 Replies

· Add your reply
  • Sort: 
avatar image
28
Best Answer

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;
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Akermay · Dec 01, 2013 at 01:18 AM 6
Share

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.

avatar image ZLR_Games Akermay · Oct 24, 2019 at 03:43 PM 0
Share

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.

avatar image j0ffe · Dec 01, 2013 at 03:20 AM 1
Share

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.

avatar image aldonaletto · Dec 01, 2013 at 12:27 PM 1
Share

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.

avatar image alisonamp · Jul 18, 2014 at 01:00 PM 0
Share

Where should I add such code?

avatar image DiogoCosta · Feb 16, 2015 at 06:44 PM 0
Share

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) '

Show more comments
avatar image
4

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
         }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KingPic · Dec 11, 2019 at 04:14 PM 3
Share

or just: Camera.main.transform.position += Camera.main.transform.forward * ScrollWheelChange;

avatar image
1

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;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JefferyWright · Jan 08, 2016 at 08:35 PM 0
Share

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;
 }
 
avatar image Menatombo JefferyWright · Aug 08, 2020 at 07:36 AM 0
Share

You need another } at the end of your script.

avatar image
1

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Zooming To Mouse Position With Scroll Wheel 3 Answers

Mouse Wheel Zoom code not working like it should 1 Answer

Character Zoom in and out 1 Answer

Camera Zoom - FoV/Mouse Sensitivity Issues 1 Answer

Can you use UI Scroll Rect for camera movement? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges