Camera rotate to look at GameObject from Raycast

Hi guys,

I’m trying to write a C# script that zooms a stationary camera via scroll wheel. I want it so that when a scrolling motion is detected, if the mouse position is over a game object, then the camera rotates towards the game object to look at it and zoom in/out. I’m using Physics.Raycast and having a few problems with accessing the position of the object that is hit for camera.transform.LookAt…

My code so far:

private const int ZoomSpeed = 7; 

    void Update() {	
    
    	if(Input.GetAxis("Mouse ScrollWheel") != 0) {
    		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    		RaycastHit hit;
                    if(Physics.Raycast(ray,out hit, Mathf.Infinity)) {
    			camera.transform.LookAt (hit.collider.gameObject.position); 
    			//This doesn't work
    		}
    	}
    		
    	var zoomDelta = Input.GetAxis("Mouse ScrollWheel")*ZoomSpeed*Time.deltaTime;
    	if (zoomDelta > 0 && Camera.main.fieldOfView >10){
    		Camera.main.fieldOfView--;
    	}
    	if (zoomDelta < 0 && Camera.main.fieldOfView <60){
    		Camera.main.fieldOfView++;
    	}
    }

Any suggestions appreciated!

Ok, here is the final version of the zoom to mouse cursor script via scroll wheel by using Lerp… This script tries to zoom towards/around the mouse cursor so that the mouse is still on the object/area it was initially pointing at (a bit like Google Maps zoom). The Lerping out is still a little buggy, however. Note that the values are tailored to my game and it sometimes shifts a little off the mouse:

Quaternion init; 
    void Start() 
    {
        init = camera.transform.rotation;
    }

    void Update () {
        	if (Input.GetAxis("Mouse ScrollWheel") < 0) 
        	{
            	camera.transform.rotation = Quaternion.Lerp(init, camera.transform.rotation, .3f);
        	}
            else if (Camera.main.fieldOfView < 50 && Input.GetAxis("Mouse ScrollWheel") < 0)
        	{
            	var startR = camera.transform.rotation;
				
            	camera.fieldOfView -= 10*Input.GetAxis("Mouse ScrollWheel");
            	camera.transform.rotation = Quaternion.Lerp(init, startR, .7f);
            
      	}
            else if (Camera.main.fieldOfView > 7 && Input.GetAxis("Mouse ScrollWheel") > 0)
        	{
            	var currentPos = Input.mousePosition;
                    currentPos.z = 7.3f;
                    var startR = camera.transform.rotation;
				
            	camera.transform.LookAt(camera.ScreenToWorldPoint(currentPos));
            	camera.fieldOfView -= 10*Input.GetAxis("Mouse ScrollWheel");
            	camera.transform.rotation = Quaternion.Lerp(transform.rotation, startR,((float)Camera.main.fieldOfView -(float)20*Input.GetAxis("Mouse ScrollWheel")) /((float)Camera.main.fieldOfView) );
            
        	}
	}

Thanks for all the help!

Try using:

camera.transform.LookAt (hit.collider.gameObject.transform);

Regarding your followup question:

You can get the world position under the mouse cursor by using…

worldPos = camera.ScreenPointToRay(mousePosition).GetPoint(distanceInWorldUnits);

As you can see, you need to provide a distance from the camera for the 3D mouse position.
This is because the mouse position is 2D screen space and obviously the world space is 3D.
A distance of 0 would put your 3D point on the cameras near plane. How to determine the distance you want the 3D point to be is up to you.