Center Camera view on Center of GameObject [solved]

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

Instead of setting targetPosition to hit.point, try setting it to hit.collider.transform.position.

Surely the line myCamera.position += 0.25*(targetPosition-myCamera.position); should be within the if(Input.GetMouseButtonDow n test otherwise the script will fire contineously and the camera wont follow commands from things like an fps walker script

I am not sure if i am using this script correctly - the on click seems to hit any mesh and once clicked i seem to rotate about a point ?!

i have attached the script to a mesh and set the my camera variable to the camera of my fps walker camera.