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

public class chickenMovement : MonoBehaviour
{
    public GameObject[] waypoints;
    public int index=0;
    public Vector3 target,distance;
    public float speed=2f;
    public Animator anim;
    // Start is called before the first frame update
    void OnEnable()
    {
        anim = GetComponent<Animator>();
        target = waypoints[index].transform.position;
        anim.SetBool("Walk", true);
    }

    // Update is called once per frame
    void Update()
    {

        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        distance = (target - transform.position);
        if(distance.magnitude < 0.1f)
        {
            if (index<waypoints.Length-1)
            {
                index++;
                transform.LookAt(waypoints[index].transform);
                target = waypoints[index].transform.position;
                
            }
            else
            {
                index = 0;
                transform.LookAt(waypoints[index].transform);
                target = waypoints[index].transform.position;
            }

        }
    }
}
