// Start is called before the first frame update
    void Start()
    {
        //goal = GameObject.FindWithTag("Player");

        manager = gameObject.GetComponent <WaypointManager>();
        manager.Init(); // TEMPORARY. MAKE WAYPOINT MANAGER STATIC AND REMOVE THIS LATER!!!

        if (goal.Count > 0)
        {
            followPath = manager.FindPath(manager.FindClosestWaypoint(transform), goal[goalCount].transform);

            if (followPath.Count > 0)
            {
                Vector3 direction = followPath[0].gameObject.transform.position - gameObject.transform.position;
                transform.rotation = Quaternion.LookRotation(direction.normalized) * angleOffset;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        /*
         *
         * TEMPORARY CONDITION!!!!!!!!!!!!!!
         *
         *
         * */
        if (gameObject.GetComponent <Ship>().testPathfind)
        {
            recalculatePath -= Time.deltaTime;
            if (followPath.Count > 0)
            {
                if (followPath[0].transform.position == transform.position)
                {
                    followPath.RemoveAt(0);
                    if (recalculatePath <= 0)
                    {
                        recalculatePath = 2f;

                        // If the player is still able to be pursued (ie. outside of turn radius)
                        if ((!isEngaged) || ((isEngaged) && (Mathf.Pow(goal[goalCount].transform.position.x - gameObject.transform.position.x, 2) + Mathf.Pow(goal[goalCount].transform.position.y - gameObject.transform.position.y, 2) + Mathf.Pow(goal[goalCount].transform.position.z - gameObject.transform.position.z, 2) > turnRadius)))
                        {
                            followPath = manager.FindPath(manager.FindClosestWaypoint(transform), goal[goalCount].transform);
                        }
                        else
                        {
                            followPath.Clear(); // Otherwise clear the list (the player is too close for turning)
                        }
                    }

                    if (followPath.Count > 0)
                    {
                        Vector3 direction = followPath[0].gameObject.transform.position - gameObject.transform.position;
                        transform.rotation = Quaternion.LookRotation(direction.normalized) * angleOffset;
                    }
                }
                else
                {
                    transform.position = Vector3.MoveTowards(transform.position, followPath[0].gameObject.transform.position, speed);
                }
            }
            else
            {
                if (goal.Count > 0)
                {
                    goalCount = (goalCount + 1) % goal.Count;
                    if (goal.Count > 1)
                    {
                        recalculatePath = 0;
                    }
                }

                if ((isEngaged) && (Mathf.Pow(goal[goalCount].transform.position.x - gameObject.transform.position.x, 2) + Mathf.Pow(goal[goalCount].transform.position.y - gameObject.transform.position.y, 2) + Mathf.Pow(goal[goalCount].transform.position.z - gameObject.transform.position.z, 2) < turnRadius))
                {
                    gameObject.GetComponent <Rigidbody>().velocity = speed * 50 * gameObject.transform.forward;
                    recalculatePath = 3f;   // Temporary cooldown. Replace this once turning is implemented
                }
                else if (recalculatePath <= 0)
                {
                    followPath = manager.FindPath(manager.FindClosestWaypoint(transform), goal[goalCount].transform);
                    if (followPath.Count > 0)
                    {
                        Vector3 direction = followPath[0].gameObject.transform.position - gameObject.transform.position;
                        transform.rotation = Quaternion.LookRotation(direction.normalized) * angleOffset;
                    }
                    recalculatePath = 2f;
                    gameObject.GetComponent <Rigidbody>().velocity = Vector3.zero;
                }
            }
        }
    }