var target : Transform;
var resetPos : Transform;
var minDist = 1.0;
var maxDist = 5.0;
var orbitSpeed = 10.0;

function FixedUpdate () {
	var targetPos = target.position;
	targetPos.y = 0.0;
	
	var pos = transform.position;
	pos.y = 0;
	
	var delta = targetPos - pos;
	var deltaMag = delta.magnitude;
	
	if (deltaMag > .01) {
		var adjustment;
		if (deltaMag < minDist) {
			adjustment= -delta.normalized * (minDist - deltaMag);
			transform.Translate(adjustment);
		}
		
		if (deltaMag > maxDist) {
			adjustment = delta.normalized * (deltaMag - maxDist);
			transform.Translate(adjustment);
		}
	}
	
	delta = targetPos - pos;
	var moveX = -Input.GetAxis("Camera X") * Time.deltaTime * orbitSpeed;
	var moveY = -Input.GetAxis("Camera Y") * Time.deltaTime * orbitSpeed;
	var right = Vector3.Cross(delta, Vector3.up).normalized;
	transform.position.x += right.x * moveX + delta.normalized.x * moveY;
	transform.position.z += right.z * moveX + delta.normalized.z * moveY;
}

function Update() {
	if (Input.GetButton("Fire3")) {
		ResetCamera();
	}
}

function ResetCamera() {
	transform.position = resetPos.position;
}