• 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
0

Answer by GermiyanBey · Nov 11, 2018 at 03:58 PM

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
         }
     }
 }
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
0

Answer by isopoly23 · Sep 17, 2018 at 04:07 PM

 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.
 } 



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
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 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 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.

  • ‹
  • 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

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

Need help implementing a zoom function 0 Answers

How can I make zoom dependent camera constraints? 1 Answer

RTS Camera - Zoom is not uniform, yet code is? 2 Answers

Mouse Wheel Zoom code not working like it should 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