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 douglasg14b · Dec 05, 2014 at 05:41 PM · 2dmousezoom

Having problems getting a zoom to mouse function working well with smoothstep, need some help.

I currently have a zoom to mouse function that allows the user to zoom towards their cursor in a 2D game. It works perfectly fine, but it would be must more aesthetic if it was smoothed out.

     public void zoomupdate(){
         float time = (Time.deltaTime) / 0.0075f;
         Vector3 camPosTemp = Camera.main.transform.position;
         Vector3 mousePosTemp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Camera.main.orthographicSize = Mathf.SmoothStep(Camera.main.orthographicSize, deltaZ, time);
         mousePosTemp = mousePosTemp - Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         if ((camPosTemp.y += mousePosTemp.y) == Camera.main.transform.position.y)
         {
             movecam = false;
         }
         if((camPosTemp.x += mousePosTemp.x) == Camera.main.transform.position.x)
         {
             movecam = false;
         }
 
         camPosTemp.x = Mathf.SmoothStep(camPosTemp.x, camPosTemp.x += mousePosTemp.x, time);
         camPosTemp.y = Mathf.SmoothStep(camPosTemp.y, camPosTemp.y += mousePosTemp.y, time);
 
         if(movecam)
         {
             Camera.main.transform.position = camPosTemp;
         }
     }

I implemented Mathf.Smoothstep(), and the zooming continues to work as expected. With a new quirk.... if you move the mouse while it is zooming (between the original location and the end location for smoothstep) the camera will start moving in the direction of the cursor.

I changed the function and made the function that detects the scroll wheel movement set the target. (essentially the same, except the target is set outside this function with: target = Camera.main.ScreenToWorldPoint(Input.mousePosition);)

     public void zoomupdate(){
         float time = (Time.deltaTime) / 0.0075f;
         Vector3 camPosTemp = Camera.main.transform.position;
         Vector3 mousePosTemp;
 
         Camera.main.orthographicSize = Mathf.SmoothStep(Camera.main.orthographicSize, deltaZ, time);
         mousePosTemp = target - Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         if ((camPosTemp.y += mousePosTemp.y) == Camera.main.transform.position.y)
         {
             movecam = false;
         }
         if((camPosTemp.x += mousePosTemp.x) == Camera.main.transform.position.x)
         {
             movecam = false;
         }
 
         camPosTemp.x = Mathf.SmoothStep(camPosTemp.x, camPosTemp.x += mousePosTemp.x, time);
         camPosTemp.y = Mathf.SmoothStep(camPosTemp.y, camPosTemp.y += mousePosTemp.y, time);
 
         if(movecam)
         {
             Camera.main.transform.position = camPosTemp;
         }
     }


This function actually causes the entire camera to move as if you were click-and-dragging while it zooms.

I think I know why this happens, but I do not know how to fix it.

Why: As the function loops from update(), each iteration changes the target location based on the mouse location AFTER the cameras orthographic size is changed. This works perfectly fine as long as the mouse does not move during the time Mathf.Smoothstep() is running. If the cursor changes locations than the target changes and the "zoom to mouse" accommodates that change by doing what it is supposed to, zooming to the mouse.

How: Predict the change in camera size (px or world units) when the orthographic size changes, and predict where the mouse's location will move to (in pc or world units). Pretty much determine how much the camera size will change with the next zoom, which allows me to make a prediction on where the mouse's location will move to, and translate the camera appropriately.

Issues I don't get:

  • How to convert between pixels on screen to world points (if the camera size is reduced by 2 pixels on each side, I need to translate it's position in the world by 2 pixels. This is not possible for me unless I know how many pixels are in a world unit...)

If I had a common unit of measurement between the camera width/height, the world point of the mouse, and world point of the camera I think I could try something.

I would appreciate any help you guys can give me!

Edit 1: Think I figured out how, just need some help. Added in my thoughts.

Thanks.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

how to drag an object using 2d raycasts 0 Answers

what is the name of the mouse wheel in the input 2 Answers

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

2D platformer cursor 1 Answer

IPhone is zoomed in/out depending on which model 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