public void ClearDestination()
 {
     StopAllCoroutines();
     NavMeshAgent.ResetPath();
     mOnFinishPath = null;
     mDestination  = Vector3.zero;
     mCountRepath  = 0;
     // Debug.Log(gameObject.name + " ClearDestination");
 }
    private IEnumerator DestinationDelay(float delay, Vector3 to, OnFinishPath onFinishPath)
    {
        yield return(new WaitForSeconds(delay));

        mOnFinishPath = onFinishPath;
        mDestination  = to;
        mCountRepath  = 0;
        NavMeshAgent.SetDestination(to);
        yield break;
    }
    // public void StopMove(float delay = -1)
    // {
    //     if(mOnFinishPath != null){
    //         Debug.LogWarning("Can't stop when Wait Action");
    //         return;
    //     }
    //     mDelayUpdate = delay;
    //     mWaitAction = Action.Stop;
    //     Debug.Log(gameObject.name + " Need_Stop");
    // }

    public void Update()
    {
        if (mDelayCheck > 0)
        {
            mDelayCheck -= Time.deltaTime;
            return;
        }
        if (mNavMeshAgent == null)
        {
            mNavMeshAgent = GetComponent <NavMeshAgent>();
        }
        if (mNavMeshAgent == null)
        {
            return;
        }
        if (mWaitAction != Action.Non)
        {
            CurrentAction = mWaitAction;
            mWaitAction   = Action.Non;
        }
        else if (mOnFinishPath != null)
        {
            if (!mNavMeshAgent.isStopped &&
                !mNavMeshAgent.pathPending &&
                mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance &&
                (!mNavMeshAgent.hasPath || mNavMeshAgent.velocity.sqrMagnitude == 0f))
            {
                Vector2 d = new Vector2(transform.position.x - mDestination.x, transform.position.z - mDestination.z);
                if (d.sqrMagnitude < MIN_D)
                {
                    // Debug.Log(gameObject.name + " OnFinishPath");
                    var callback = mOnFinishPath;
                    mOnFinishPath = null;
                    callback(mNavMeshAgent);
                    CurrentAction = mCurrentAction;
                }
                else
                {
                    if (mCountRepath <= 1)
                    {
                        SetDestination(mDestination);
                        mCountRepath++;
                    }
                    else
                    {
                        // Debug.Log(gameObject.name + " OnFinishPath");
                        var callback = mOnFinishPath;
                        mOnFinishPath = null;
                        callback(mNavMeshAgent);
                        CurrentAction = mCurrentAction;
                    }
                }
            }
        }
    }
 public void DestinationTo(float delay, Vector3 to, OnFinishPath onFinishPath)
 {
     if (delay <= 0)
     {
         mDelayCheck   = -1;
         mOnFinishPath = onFinishPath;
         mDestination  = to;
         mCountRepath  = 0;
         SetDestination(to);
     }
     else
     {
         StopAllCoroutines();
         StartCoroutine(DestinationDelay(delay, to, onFinishPath));
     }
 }
    public void WalkTo(float delay, Vector3 to, OnFinishPath onFinishPath)
    {
        float lastSpeed = NavMeshAgent.speed;

        NavMeshAgent.speed = 2;
        float lastStoppingDistance = NavMeshAgent.stoppingDistance;

        NavMeshAgent.stoppingDistance = 0.2f;
        this.GetComponent <BasicMecanimControl>().IsRun = false;
        this.DestinationTo(delay, to, (NavMeshAgent nav) =>
        {
            NavMeshAgent.speed            = lastSpeed;
            NavMeshAgent.stoppingDistance = lastStoppingDistance;
            this.GetComponent <BasicMecanimControl>().IsRun = true;
            if (onFinishPath != null)
            {
                onFinishPath(nav);
            }
        });
    }