var xSpeed = 120.0;
var ySpeed = 120.0;
var zSpeed = 120.0;
var yRotationSpeed = 20.0;

var yMin = 20;
var yMax = 80;

private var x = 0.0;
private var y = 0.0;
private var z = 0.0;
private var xAngle = 0.0;
private var yAngle = 0.0;
private var zAngle = 0.0;

@script AddComponentMenu("Camera-Control/Overhead (David)")

function Start () 
{
    x = transform.position.x;
    y = transform.position.y;
    z = transform.position.z;
    xAngle = transform.eulerAngles.x;
    yAngle = transform.eulerAngles.y;
    zAngle = transform.eulerAngles.z;
}

function LateUpdate () 
{
	if (!camera.enabled)
		return;
		
	if (Input.GetButton("Fire2"))
	{
		// If one of the shift keys is pressed, mouse movement will control the
		// rotation around the y axis
		if (Input.GetKey(KeyCode.LeftShift) | Input.GetKey(KeyCode.RightShift))
		{
			yAngle += Input.GetAxis("Mouse X") * yRotationSpeed;
			y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
		}
		
		// Otherwise, they control the camera movement on the XZ plane
		else
		{
			// Unlike the 3rd Person camera script, Mouse Y controls z movement
			// and ScrollWheel controls y movement
	        var xDelta = Input.GetAxis("Mouse X") * xSpeed * 0.02;
	        var zDelta = Input.GetAxis("Mouse Y") * zSpeed * 0.02;
	        var deltaVec = Quaternion.Euler(0, yAngle, 0) * Vector3(xDelta, 0, zDelta);
	        x += deltaVec.x;
	        z += deltaVec.z;
	    }
	    
	    // Y scrolling is independent of shift key
	    //y -= Input.GetAxis("Mouse ScrollWheel") * zSpeed * 0.02;
		
		y = Mathf.Clamp(y, yMin, yMax);
		yAngle = ClampAngle(yAngle);
		 		       
	    var rotation = Quaternion.Euler(xAngle, yAngle, zAngle);
	    var position = Vector3(x, y, z);
	    
	    transform.rotation = rotation;
	    transform.position = position;
	}
}

static function ClampAngle (angle : float)
{
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return angle;
}