lerp problem and trying to understand "return;"

While I hover over an collider. The camera moves in on the object but only while I keep my mouse over it. What I want is that I only have to click on the collider to get the camera to move towards it.

#pragma strict

var cam : GameObject;
var camStartPos : Vector3;

function Start()
{
	cam = GameObject.Find("Main Camera");
}

function Update ()
{
	var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	
	if (collider.Raycast (ray, hit, 100.0)) 
	{
		while(cam.transform.position.z < 7)
		{
		cam.transform.position = Vector3.Lerp (cam.transform.position, transform.position,Time.deltaTime * 1);
		return;
		}
	}
}

Also, Could someone explain why the “return;” works here. Why does it just teleport the camera without the “return” ?..I just dont get it. Looked in the docs but that didnt help

For the first question, just put a conditional in before the raycast bit, checking for mouse input:

if(Input.GetMouseButtonDown(0))
{
    // Do the rest of the things
}

The ‘return’ causes the function to immediately stop, instead of completing the while loop. In this case, this is no different from using

if(cam.transform.position.z < 7)
{
    cam.transform.position = Vector3.Lerp (cam.transform.position, transform.position,Time.deltaTime * 1);
}

since the ‘return’ makes sure that it can only happen once anyway. As a general rule of thumb, unless you are trying to make some kind of instantaneous change, you shouldn’t use while loops in the Update function for behaviour modelled over time- each Update must execute once per frame, and if you have a while loop inside there, it will run all the way through the loop before moving on to the next frame- hence the teleporting.

This is why you shouldn’t use things like

while(Input.GetButtonDown("Button"))
{
    // do things
}

as this will cause an infinite loop, because ‘GetButtonDown’ won’t get reset until the Update finishes!

This is a weird piece of code, for sure. The return instruction exits the current function - Update, in this case. It’s there to avoid an infinite loop in some cases, and has the collateral effect of ensure a call to Lerp each frame. There are lots of smarter ways to do that.

If you want to move the camera to the clicked object, you can use something like this:

#pragma strict

var distance: float = 5;
var speed: float = 1.5;
private var cam : Transform;
private var target: Transform;

function Start(){
    cam = GameObject.Find("Main Camera").transform;
}

function Update (){
  if (Input.GetMouseButtonDown(0)){ // if mouse button pressed...
    var ray : Ray = cam.camera.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit; // do a raycast from the mouse position
    if (Physics.Raycast (ray, hit, 100.0)){ // if something hit...
      target = hit.transform; // define the target object
    }
  }
  if (target){ // if some target object defined...
    // calculate a position at "distance" from the target object
    var tgtPos: Vector3 = target.position - distance * cam.forward;
    // move to the position a little step each frame
    cam.position = Vector3.Lerp (cam.position, tgtPos, Time.deltaTime * speed);
  }
}