using UnityEngine;
using System.Collections;

public class SmoothLookAt : MonoBehaviour
{
	public Transform target;
	public float damping = 6.0f;
	public bool smooth = true;
	public float minDistance = 10.0f;

	private float alpha = 1.0f;
	private Transform _myTransform;

	private void Awake()
	{
		_myTransform = transform;
	}

	private void LateUpdate()
	{
		if(target)
		{
			if(smooth)
			{
				Quaternion rotation = Quaternion.LookRotation(target.position - _myTransform.position);
				_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, rotation, Time.deltaTime * damping);
			}
			else
			{
				_myTransform.rotation = Quaternion.FromToRotation(-Vector3.forward, (new Vector3(target.position.x, target.position.y, target.position.z) - _myTransform.position).normalized);

				float distance = Vector3.Distance(target.position, _myTransform.position);

				if(distance < minDistance)
				{
					alpha = Mathf.Lerp(alpha, 0.0f, Time.deltaTime * 2.0f);
				}
				else
				{
					alpha = Mathf.Lerp(alpha, 1.0f, Time.deltaTime * 2.0f);
				}
			}
		}
	}
}
