Rotating camera script won't work

In my Unity project I’m trying to create a main menu where moving the camera will smoothly pan from one object to another as the user scrolls up and down the menu. I made a script for this but for some reason it just turns into an infinite loop. I’m somewhat lack experience both with unity and javascript and I don’t know why this happens. Any solution or better method would be greatly appreciated.

var a = 1;

function Update ()
{
	if(Input.GetButtonDown("w"))
		{
			while (a>0)
				{
				transform.Rotate(0,15*Time.deltaTime,0);
				var b = 15*Time.deltaTime;
				if (b>90)
					{
					var a = 0;
					}
				}
		
		}
}

For starters, you have a while-loop inside of Update. That’s just asking for trouble. Remember that Update gets called once per frame- if you want behaviour to happen over several frames, you can’t wait inside a single Update call for it to finish.

You should use more descriptive variable names. ‘a’ and ‘b’ don’t mean anything- not to mention the fact that you are declaring ‘a’ twice (meaning that the second one, the one inside the if-statement, will conflict with the first one within that scope, and do nothing outside of it). Just like all things coding, you need to look carefully at what you have written, and step through what it should do. For example, b will only ever become >90 in the situation when you are running at about 0.3 frames per second- a pretty unusual circumstance.

In any case, Unity provides us with a few convenient ways to model behaviour over time. In this case, I would recommend using a Coroutine- this sets off a process that can be suspended temporarily, allowing us to wait a frame before moving on to the next step.

This is a javascript coroutine that rotates the transform around the y axis by ‘angle’ degrees at ‘speed’ degrees per second:

function RotateObject(angle : float, speed : float)
{
    if(speed == 0)
    {
       // Avoid a divide-by-zero error!
        return;
    }
    var startRot : Quaternion = transform.rotation;
    var endRot : Quaternion = startRot * Quaternion.angleAxis(angle, Vector3.up);
    var totalTime = Mathf.Abs(angle / speed);
    var curTime = 0;
    while(curTime < totalTime)
    {
        curTime += Time.deltaTime;
        transform.rotation = Quaternion.Slerp(startRot, endRot, curTime / totalTime);
        yield;
    }
}

Then, to activate this with a 90-degree turn over 6 seconds, call it like this:

if(Input.GetButtonDown("w")) // By the way, "w" isn't a normal button name.
                             // If you're using it, make sure it's set up
                             // in your input settings.
{
    StartCoroutine(RotateObject(90, 15));
}