using UnityEngine;
using System.Collections;

public class FollowTargetAtPercentageScript : MonoBehaviour 
{
	public Transform target;
	public bool followX;
	public bool followY;
	public bool followZ;
	public float relativeSpeed;

	public Vector3 previousVector;
	private Vector3 deltaVector;
	
	private Transform myTransform;
	
	void Awake()
	{
		myTransform = transform;
	}
	
	// Use this for initialization
	void Start () 
	{
		Initialize();
	}
	
	void OnEnable ()
	{
		Initialize();
	}
	
	public void Initialize()
	{
		previousVector = target.position;
	}
	
	// Update is called once per frame
	void Update ()
	{
		deltaVector = new Vector3(0,0,0);
		if (followX)
		{
			deltaVector.x = target.position.x - previousVector.x;
		}
		if (followY)
		{
			deltaVector.y = target.position.y - previousVector.y;
		}
		if (followZ)
		{
			deltaVector.z = target.position.z - previousVector.z;
		}
		
		Vector3 pixelPerfectVector = new Vector3(PixelPerfect.RoundToNearestPixel((deltaVector.x  * relativeSpeed), Camera.main), PixelPerfect.RoundToNearestPixel((deltaVector.y  * relativeSpeed), Camera.main), PixelPerfect.RoundToNearestPixel((deltaVector.z  * relativeSpeed), Camera.main));
		
		myTransform.Translate(pixelPerfectVector);
		
		previousVector = target.position;
	}
}
