- Home /
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!
camera.transform.LookAt (hit.collider.gameObject.transform); worked! (Thank you!)
Follow-up question: I'm wondering if there's a way to get the camera to zoom in/out to any spot that the mouse is over (not necessarily at game object)?
Answer by Seizure · Aug 01, 2013 at 03:26 PM
Try using:
camera.transform.LookAt (hit.collider.gameObject.transform);
Answer by mntng · Aug 02, 2013 at 02:55 PM
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!
Answer by FuzzyLogic · Aug 01, 2013 at 04:19 PM
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.
Your answer
Follow this Question
Related Questions
Make the terrain ignore Raycast if in between Camera and Player? 1 Answer
Pinch zoom 0 Answers
How to move the camera closer to the player while there is an object between them? 1 Answer
Mouseclick GameObject through the view of a RenderTexture 0 Answers
How to turn camera using LookAt() only along X and Y axis? 1 Answer