• 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 $$anonymous$$ · Dec 28, 2018 at 02:46 PM

Why are people still answering this? ☺

 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;
     }
 }
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
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 ow3n · Dec 28, 2018 at 01:22 PM

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


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 Mr_Mystery · Jul 15, 2020 at 04:51 AM

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

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