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

// This class can be used to have an agent face the direction it's moving and at the end of movement face the direction that you specify
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerAgentFacingFixer : MonoBehaviour
{
    public Quaternion FinalFacing = Quaternion.identity;

    private bool wasMoving = false;
    private NavMeshAgent agent = null;

    private void Start()
    {
        wasMoving = false;
        agent = GetComponent<NavMeshAgent>();
        
    }

    // Update is called once per frame
    void Update()
    {
        float dist = agent.remainingDistance;
        if (dist != Mathf.Infinity && agent.pathStatus == NavMeshPathStatus.PathComplete && agent.remainingDistance == 0)
        {
            if (wasMoving)
            {
                // Fix up final rotation
                gameObject.transform.rotation = FinalFacing;
            }
        }
        else
        {
            wasMoving = true;

            //var targetPosition = agent.pathEndPosition;
            //var targetPoint = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
            //var _direction = (targetPoint - transform.position).normalized;
            //var _lookRotation = Quaternion.LookRotation(_direction);

            //transform.rotation = Quaternion.RotateTowards(transform.rotation, _lookRotation, 360);
        }
    }
}
