예제 #1
0
        private IEnumerator WayPointBehavior(WayPoints wayPoints)
        {
            Transform[] points   = wayPoints.points;
            float       waitTime = wayPoints.uponReachWaitTime;
            bool        walk     = wayPoints.walk;
            bool        repeat   = wayPoints.patrol;
            bool        stopLoop = false;

            int       currentIndex       = FindCurrentClosestPoint(points);
            int       startingIndex      = currentIndex;
            Transform currentTargetPoint = points[currentIndex];

            _navAgent.SetDestination(currentTargetPoint.position);
            _animator.SetInteger(WalkId, walk ? 1 : 2);

            while (!stopLoop)
            {
                // Upon Reaching the Point, Wait for some seconds and Move On
                if (Vector3.Distance(transform.position, points[currentIndex].position) <= _stoppingDistance)
                {
                    _animator.SetInteger(WalkId, 0);

                    yield return(new WaitForSeconds(waitTime));

                    currentIndex = (currentIndex + 1) % points.Length;

                    if (!repeat && currentIndex == startingIndex)
                    {
                        stopLoop = true;
                    }

                    else
                    {
                        currentTargetPoint = points[currentIndex];
                        _navAgent.SetDestination(currentTargetPoint.position);
                        _animator.SetInteger(WalkId, walk ? 1 : 2);
                    }
                }

                else
                {
                    yield return(new WaitForSeconds(0.2f));
                }
            }
        }
예제 #2
0
 public void StartWayPointBehavior(WayPoints wayPoints)
 {
     ClearBehavior();
     StartCoroutine(WayPointBehavior(wayPoints));
 }