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 :frowning:

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);	
			}
		}
	}
}

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);

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.

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);
		}
	}