Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by scopique · Sep 24, 2012 at 07:05 PM · cameramousezoom

Zooming To Mouse Position With Scroll Wheel

I'm looking to zoom my camera into the location of mouse in world space. I HAD code to do this from...somewhere, but I lost my project during a reformat, and my backups were older than I'd like. Derp :(

Currently, the camera is rotated at a 45 degree angle on the X axis, and is at (0,10,-5) for position. It's looking down at this angle on the "field" where GameObjects are instantiated at run time.

I have a script which currently zooms in along the Z axis using the SPACE.SELF, but it only zooms in to where the camera is currently pointing (and has limits to how far and how close it can get). However, I'd like the camera to zoom in while at the same time moving to center on where the mouse pointer is (in world-space). Basically, to allow a user to point, zoom, and center all in one motion, with limits on how close or how far the zoom can get.

Can anyone point me to any known code that does this, or offer some pointers? Here's my CURRENT forward-zoom code, for what it's worth.

Thanks!

 using UnityEngine;
 using System.Collections;
 
 public class CameraZoom : MonoBehaviour {
     
     public float distanceMin;
     public float distanceMax;
     public float zoomSpeed;
         
     private float[] distances = new float[32];
     private Vector3 moveDirection = Vector3.zero;
         
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         moveDirection = new Vector3(0,0,Input.GetAxis("Mouse ScrollWheel"));
         moveDirection *= zoomSpeed;
         
         //Checking the upper and lower bounds is a matter of determining if 
         //    the direction we're moving is positive (zooming in) or negative (zooming out). 
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             //We shouldn't be allowed to zoom in more than distanceMin
             if (Mathf.Floor(transform.position.y + moveDirection.y) > distanceMin)
             {
                 transform.Translate(moveDirection, Space.Self);
                 
             }
         }else if (Input.GetAxis("Mouse ScrollWheel") < 0){
             //We shouldn't be allowed to zoom out more than distanceMax
             if (Mathf.Floor(transform.position.y + moveDirection.y) < distanceMax)
             {
                 transform.Translate(moveDirection, Space.Self);    
             }
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Jaxom · Feb 21, 2016 at 04:35 PM

if (Input.GetAxis("Mouse ScrollWheel") != 0) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit point; Physics.Raycast(ray, out point, 25); Vector3 Scrolldirection = ray.GetPoint(5);

             float step = zoomSpeed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, Scrolldirection, Input.GetAxis("Mouse ScrollWheel") * step);
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
2

Answer by WalterEspinar · Apr 26, 2019 at 02:20 PM

Best of two worlds

     public float zoomSpeed;
     public float minZoom;
     public float maxZoom;
 
     void Update()
     {
         Zoom();
     }
 
     void Zoom()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit point;
         Physics.Raycast(ray, out point, 1000);
         Vector3 Scrolldirection = ray.GetPoint(5);
 
         float step = zoomSpeed * Time.deltaTime;
 
         // Allows zooming in and out via the mouse wheel
         if (Input.GetAxis("Mouse ScrollWheel") > 0 && Scrolldirection.y > minZoom)
         {
             transform.position = Vector3.MoveTowards(transform.position, Scrolldirection, Input.GetAxis("Mouse ScrollWheel") * step);
         }
         if (Input.GetAxis("Mouse ScrollWheel") < 0 && Scrolldirection.y < maxZoom)
         {
             transform.position = Vector3.MoveTowards(transform.position, Scrolldirection, Input.GetAxis("Mouse ScrollWheel") * step);
         }
     }

  
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 T27M · Sep 24, 2012 at 07:44 PM

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
 RaycastHit hit;
         
 if( Physics.Raycast(ray, out hit, 100) )
     Debug.DrawLine(ray.origin, hit.point, Color.green);
 
             // Allows zooming in and out via the mouse wheel
             if(Input.GetAxis("Mouse ScrollWheel") < 0)
             {
                 transform.Translate(0, 1, 0, Space.World);
                 transform.position = new Vector3 (transform.position.x ,Mathf.Clamp(transform.position.y,6,10), transform.position.z) ;                    
             }                
             if(Input.GetAxis("Mouse ScrollWheel") > 0)
             {
                 transform.Translate(0,-1,0, Space.World);
                 transform.position = new Vector3 (hit.point.x ,Mathf.Clamp(transform.position.y,6,10), hit.point.z);
             }


This will zoom to the mouse position but its not 100% as the mouse position moves as your zooming in, it also zooms straight out from the location it is at. You can easily restrict the Y axis by changing the values in Mathf.clamp() and the speed that the camera moves via the transform.translate.

I'm trying to work out a way to get the hit position and zoom to it without updating the position mid zoom.

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 T27M · Sep 24, 2012 at 07:48 PM 0
Share

I suppose one way to do it, would be to zoom to the location in one go rather than smaller movements.

avatar image scopique · Sep 24, 2012 at 11:30 PM 0
Share

I had tried working with raycasting earlier in the day, but the (conceptual, at least) issue I kept co$$anonymous$$g around to was that there was nothing to hit. Ideally, a user could zoom in on an "empty" section of the screen between multiple instantiated items to view them all within the viewport. I could use a plane, maybe, beneath the items.

And I actually DO have another script which allows the user to click on an item to instantly move the camera to that one specific item, but I'm looking for something that reduces the need to move the camera manually, and then zoom.

avatar image T27M · Sep 25, 2012 at 03:10 PM 0
Share

Hmm if i understand correctly what you are trying todo then you could cast a ray from the camera to the mouse position and then zoom towards that direction rather than a point, maybe use the closest object as some sort of guide for the camera.

Just an idea I havent tested it yet.

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

14 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

Related Questions

How do I make the camera zoom in and out with the mouse wheel? 10 Answers

Camera Zoom - FoV/Mouse Sensitivity Issues 1 Answer

Understanding Gun Scopes & Fov 1 Answer

how to show cursor on specific time? 2 Answers

Camera Rotation 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