using UnityEngine;
using System.Collections;

public class RunAfterKill : MonoBehaviour
{

    public float speed;
    public GameObject target;

    public float timeRunning;

    private float _timeWaiting = 0;
    private bool _waiting = true;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(AreDead());


    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(_timeWaiting + " - " + timeRunning);
        if (_waiting)
        {
            _timeWaiting += Time.deltaTime;

            if (_timeWaiting >= timeRunning)
            {
                _waiting = false;
                GetComponent<Animator>().SetInteger("Special", 63);
                GetComponent<AudioSource>().Play();
                GameObject.Find("Soldier2").GetComponent<Animator>().SetInteger("Special", 4);
                Destroy(this);
            }
        }

        float l_hDir = target.transform.position.x - gameObject.transform.position.x;
        float l_vDir = target.transform.position.y - gameObject.transform.position.y;
        Vector3 l_vector = new Vector3(l_hDir, l_vDir, l_vDir);
        l_vector = l_vector.normalized;

        transform.Translate(l_vector * speed * Time.deltaTime);
    }

    IEnumerator AreDead()
    {
        yield return new WaitForSeconds(timeRunning);
        GetComponent<Animator>().SetInteger("Special", 63);
        GetComponent<AudioSource>().Play();
        GameObject.Find("Soldier2").GetComponent<Animator>().SetInteger("Special", 4);
        Destroy(this);
    }
}
