I'm trying to figure out what I need to write in order to get the camera to center on a gameobject (this case a cube) after the user as clicked on it. I have a script that works close to what I need;however, it goes to the mouse's location.
I'm not sure what do would be best: to twick the raycast script applied to the main camera, or edit it to act with another script attached to gameobjects. I was thinking if I went with the second, it maybe easier since there is more than one cube in scene.
If someone could help me figure out what to do, that would be awesome. And if I need to explain a little more about the project I'm trying to work on, I can do so.
Here's the raycast script I have.
#pragma strict
var myCamera : Transform;
var targetPosition : Vector3;
var zoomedIn : boolean;
var defaultPosition : Vector3;
var camOffset : Vector3;
function Start(){
targetPosition = myCamera.position;
}
function Update () {
if(Input.GetMouseButtonDown(0)){
if(zoomedIn){
targetPosition = defaultPosition;
zoomedIn = false;
}
else{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray,hit, 50)) {
//targetPosition = hit.point;
var hitPosition = hit.transform.position;
targetPosition = hitPosition+camOffset;
zoomedIn =true;
}
}
}
myCamera.position += 0.25*(targetPosition-myCamera.position);
}