using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour {

    public Transform target;
    public float moveSpeed;
    public float acceptableYOffset;
    public float rotationSpeed;
    private Vector2 inicialPosition;

    // Use this for initialization
    void Start()
    {
        inicialPosition = transform.localPosition;
        moveSpeed = moveSpeed + transform.GetSiblingIndex();
    }

    // Update is called once per frame
    void Update()
    {
        float currentDistance = Mathf.Abs(target.position.y) - Mathf.Abs(transform.position.y);
        currentDistance = Mathf.Abs(currentDistance);
        if (currentDistance > acceptableYOffset)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
            //Vector3 vectorToTarget = target.transform.position - transform.position;
            //float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
            //Quaternion qt = Quaternion.AngleAxis(angle, Vector3.forward);
            //transform.rotation = Quaternion.RotateTowards(transform.rotation, qt, Time.deltaTime * rotationSpeed);
        }
        else {
            transform.localPosition = inicialPosition;
        }
    }
}

