using UnityEngine;
using System.Collections;

public class FollowTargetScript : MonoBehaviour 
{
	public Transform target;
	public bool followX;
	public bool followY;
	public bool followZ;

	private 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() 
	{
		if(target != null)
		{
			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;	
		}
		
		myTransform.Translate(deltaVector);
		
		previousVector = target.position;
	}
}