- Home /
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);
}
}
}
}
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);
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);
}
}
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.
I suppose one way to do it, would be to zoom to the location in one go rather than smaller movements.
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.
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
Follow this Question
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