- Home /
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.
Your answer
Follow this Question
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