Camera Point-and-click Movement

I'm a noob at scripting and haven't seen what I would like to be able to do. Or maybe I have and it hasn't dawned on me....

The idea is to use a script applied to many different cubes that allows a cube to be clicked. When clicked the camera should snap to a location directly in front of that cube. When clicked again, the camera should snap back to its initial location.

I've figured out how how to get a screen cross fade to work between 2 cameras; however, I need to be able to use just the main camera. Any help would be greatly appreciated.

Thanks.

This is the bit of code I think you want: EDIT: Hacked to solve your comment.

var myCamera : Transform;
var targetPosition : Vector3;
var zoomedIn : boolean;
var defaultPosition : Vector3;

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)) {
                Debug.Log(hit.point);
                targetPosition = hit.point;
                zoomedIn =true;
            }
        }
    }

    myCamera.position += 0.25*(targetPosition-myCamera.position);
}

From RaycastHit, you can detect which object you have hit, and also get the point of the hit, allowing you to move the camera.

I had help in another one of my questions, but here's the code that I'm working with applied to the camera that will offset from the object clicked.


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