
public var dieName = "SixSidedDie";
public var allowedDistance = 0.5;
private var lastDieValue = 0;

private var target : Transform;
public var damping = 3.0;

private var dieValueComponent;

public var currentLocation = 1;
public var targetLocation = 1;

function Start() {
	// cache the dieValueComponent
    dieGameObject = GameObject.Find(dieName);
	dieValueComponent = dieGameObject.GetComponent("DieValue");
	
	// look for the starting position and move there
	tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
	transform.position = tileGameObject.transform.position;
	transform.rotation = tileGameObject.transform.rotation;
}

function Update ()
{
	if (dieValueComponent.playStarted) {
		// get the new transform if necessary
		// look for a non zero number which means the die has a value
		// block any value that has already been calculated, hence checking last value
		if (lastDieValue != dieValueComponent.currentValue && dieValueComponent.currentValue != 0 && dieValueComponent.turnBegun) {
			targetLocation += dieValueComponent.currentValue;
			currentLocation += 1;
			if (currentLocation > 16) currentLocation = 16;
			if (targetLocation > 16) targetLocaiton = 16;
			tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
			target = tileGameObject.transform;
			dieValueComponent.turnBegun = false;
		}
		if (lastDieValue != 0) {
			dist = Vector3.Distance(transform.position, target.position);
			if (dist > allowedDistance) {
				transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * damping);	
				transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, Time.deltaTime * damping);	
			}
			else if (currentLocation != targetLocation) {
				// reset the target
				currentLocation += 1;
				if (currentLocation > 16) currentLocation = 16;
				tileGameObject = GameObject.Find("tile" + currentLocation.ToString() + "target" );
				target = tileGameObject.transform;
			}
		}
		lastDieValue = dieValueComponent.currentValue;
	}
}