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
2
Question by siege911 · Jan 21, 2013 at 04:32 PM · camerazoomorthographic

Ortho camera zoom to mouse point

I've got a simple script to change the orthographic size of my camera when I scroll in or out on the mouse wheel. The only problem is that it basically zooms straight in and out.

I want to be able to zoom towards the mouse point. So if my mouse is over an object, it zooms right in on that object. I'm unsure how to start on that given that it's an orthographic camera so the camera itself isn't moving towards a point.

Comment
Add comment
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

4 Replies

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

Answer by siege911 · Jan 21, 2013 at 05:38 PM

After a bunch more reading and playing around with code, I figured out exactly how to do what I'm trying to accomplish. It works for both zoom in and out. It moves the camera so that the mouse stays on the exact same 2D location as you zoom in or out.

Here's the rough code for whoever may need it. It's got some redundant code and needs to be cleaned up as some of it is very specific to my application, but it's a good basic start for anyone who needs it.

 //ScrollWheel to zoom in and out.
     if( Input.GetAxis("Mouse ScrollWheel") > 0 && camera.orthographicSize > cameraMin)
     {
         mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
         dragOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
         transform.position.y = transform.position.y + ((mousePos.y - dragOrigin.y) / (camera.orthographicSize * 2));
         transform.position.x = transform.position.x + ((mousePos.x - dragOrigin.x) / (camera.orthographicSize * 2));
         transform.position.z = -15;
         
         camera.orthographicSize -= .5;
         camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
 
         
     }
     else if(Input.GetAxis("Mouse ScrollWheel") < 0 && camera.orthographicSize < cameraMax)
     {
         mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, camera.transform.position.z));
         dragOrigin = camera.ScreenToWorldPoint(Vector3 (Screen.width / 2, Screen.height / 2, camera.transform.position.z));
         transform.position.y = transform.position.y - ((mousePos.y - dragOrigin.y) / (camera.orthographicSize * 2));
         transform.position.x = transform.position.x - ((mousePos.x - dragOrigin.x) / (camera.orthographicSize * 2));
         transform.position.z = -15;
         }
         mousePos = camera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0 - camera.transform.position.z));
         dragOrigin = camera.transform.position;

         camera.orthographicSize += .5;
         camera.orthographicSize = Mathf.Clamp(camera.orthographicSize, cameraMin, cameraMax);
 
     }
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 Guillaumzed · Oct 28, 2015 at 08:19 AM 0
Share

$$anonymous$$athematics isn't my first langage, I searched a way to do that for HOURS. Thanks a bunch.

avatar image
14

Answer by MasterKelli · Jul 16, 2014 at 09:09 AM

Even though this thread is very old, here's my clean version of zooming orthographic camera towards any point:

     // Ortographic camera zoom towards a point (in world coordinates). Negative amount zooms in, positive zooms out
     // TODO: when reaching zoom limits, stop camera movement as well
     void ZoomOrthoCamera(Vector3 zoomTowards, float amount)
     {
         // Calculate how much we will have to move towards the zoomTowards position
         float multiplier = (1.0f / this.camera.orthographicSize * amount);
 
         // Move camera
         transform.position += (zoomTowards - transform.position) * multiplier; 
 
         // Zoom camera
         this.camera.orthographicSize -= amount;
 
         // Limit zoom
         this.camera.orthographicSize = Mathf.Clamp(this.camera.orthographicSize, minZoom, maxZoom);
     }

Example with mouse:

         // Scroll forward
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             ZoomOrthoCamera(Camera.main.ScreenToWorldPoint(Input.mousePosition), 1);
         }
 
         // Scoll back
         if (Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             ZoomOrthoCamera(Camera.main.ScreenToWorldPoint(Input.mousePosition), -1);
         }

Comment
Add comment · Show 3 · 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 Victor Garcia · Sep 25, 2014 at 02:56 AM 0
Share

thank you for the clean version of the code, works as a charm.

avatar image SoulGameStudio · Jun 08, 2016 at 04:59 PM 0
Share

Excellent, thanks for this short & clean code! It prevented a bad headache

avatar image ladismad · Jan 10, 2021 at 09:57 PM 1
Share

Very old but thanks for this. Just to say, to prevent movement when at zoom limits, you can clamp amount to [size - maxZoom, size - $$anonymous$$Zoom], where size is the camera's orthographic size.

avatar image
3

Answer by Vivien_Lynn · Oct 29, 2020 at 12:45 PM

Instead of using a multiplier like @MasterKelli presented, we take the difference of the cursor position before and after zooming to change the camera position. I personally find this easier to understand and easier to debug, because we only move the camera if we actually do change the orthographic size of the camera.

So in conclusion there is no need to check if we actually clamped the zoom value. We just do clamp it and if we hit min or max, the difference between the two positions will change accordingly.

 using UnityEngine;    
 public class CameraZoom : MonoBehaviour
 {
     [SerializeField] private Camera cam;
     [SerializeField] private float zoomSpeed = 20f;
     [SerializeField] private float minCamSize = 5f;
     [SerializeField] private float maxCamSize = 20f;
 
     private void Update()
     {
         Zoom();
     }
     
     private void Zoom()
     {
         // Get MouseWheel-Value and calculate new Orthographic-Size
         // (while using Zoom-Speed-Multiplier)
         float mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
         float newZoomLevel = cam.orthographicSize - mouseScrollWheel;
 
         // Get Position before and after zooming
         Vector3 mouseOnWorld = cam.ScreenToWorldPoint(Input.mousePosition);
         cam.orthographicSize = Mathf.Clamp(newZoomLevel, minCamSize, maxCamSize);
         Vector3 mouseOnWorld1 = cam.ScreenToWorldPoint(Input.mousePosition);
 
         // Calculate Difference between Positions before and after Zooming
         Vector3 posDiff = mouseOnWorld - mouseOnWorld1;
 
         // Add Difference to Camera Position
         Vector3 camPos = cam.transform.position;
         Vector3 targetPos = new Vector3(
             camPos.x + posDiff.x,
             camPos.y + posDiff.y,
             camPos.z);
 
         // Apply Target-Position to Camera
         cam.transform.position = targetPos;
     }
 }
Comment
Add comment · Show 3 · 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 c-Row · Feb 28, 2021 at 08:39 PM 0
Share

Perfect solution - this deserves more votes!

avatar image Vivien_Lynn · Mar 03, 2021 at 12:24 PM 0
Share

I noticed a general issue with just changing the orthographic size (os) in fixed steps. Let's say our start os is 20 and we zoom in by 2. That is a difference of 10%. So we just zoomed in a little.

Now, let's do the same while we are already far zoomed in. Current os is 4, and we zoom in by 2. That is a difference of 50%. Basically everything on the screen is now twice as big as it was with the os value of 4.

To fix that, we need to zoom in percent-steps, e.g. 10%-steps.

For zoo$$anonymous$$g in, we would need to decrease the current os by 10%.

For zoo$$anonymous$$g out we would need to increase the current os by 11,11~%.

In the above example we would need to change line 19 to this:

 float newZoomLevel = mouseScrollWheel > 0
     ? cam.orthographicSize * (1 - (0.1f * mouseScrollWheel))
     : cam.orthographicSize * (1 - (0.11111f * mouseScrollWheel));
avatar image qsleonard · Jun 02, 2021 at 11:56 AM 0
Share

Excellent solution! Thank u very much ^^

avatar image
0

Answer by robertbu · Jan 21, 2013 at 04:40 PM

You'll need to translate the camera so that its x,y (assuming you are looking down the z axis) matches the x,y of the object. You can use Vector3.Lerp() so that the position changes as you zoom in. You'll have to figure out what zooming out means...zoom out from the new position or back to some original position.

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

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

16 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

Related Questions

flickering lines when zooming fov (both GUI and ortho cameras). 2 Answers

How to make a smash bros-like camera 3 Answers

Dynamic Orthagraphic Camera Zoom 1 Answer

How to zoom in smoothly with orthographic camera? 1 Answer

zoom in with orthografic camera with focus on the a specific point 0 Answers


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