Пример #1
0
        public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint)
        {
            if (_connections.Count == 0)
            {
                Debug.LogError("Insufficient waypoint count");
                return(null);
            }
            else if (_connections.Count == 1 && _connections.Contains(previousWaypoint))
            {
                return(previousWaypoint);
            }
            else
            {
                ConnectedWaypoint nextWaypoint;
                int nextIndex = 0;

                do
                {
                    nextIndex    = UnityEngine.Random.Range(0, _connections.Count);
                    nextWaypoint = _connections[nextIndex];
                } while (nextWaypoint == previousWaypoint);

                return(nextWaypoint);
            }
        }
        private void SetDestination()
        {
            if (_waypointsVisited > 0)
            {
                ConnectedWaypoint nextWaypoint = _currentWaypoint.NextWaypoint(_previousWaypoint);
                _previousWaypoint = _currentWaypoint;
                _currentWaypoint  = nextWaypoint;
            }

            Vector3 targetVector = _currentWaypoint.transform.position;

            _navMeshAgent.SetDestination(targetVector);
            _travelling = true;
        }
Пример #3
0
        // Use this for initialization
        void Start()
        {
            //get all waypoints
            GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

            //create a list of waypoints
            _connections = new List <ConnectedWaypoint>();

            //check to see if they are a connected waypoint
            for (int i = 0; i < allWaypoints.Length; i++)
            {
                ConnectedWaypoint nextWaypoint = allWaypoints[i].GetComponent <ConnectedWaypoint>();

                if (nextWaypoint != null)
                {
                    if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= _connectivityRadius && nextWaypoint != this)
                    {
                        _connections.Add(nextWaypoint);
                    }
                }
            }
        }
        // Use this for initialization
        void Start()
        {
            _navMeshAgent = this.GetComponent <NavMeshAgent>();

            if (_navMeshAgent == null)
            {
                Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name);
            }
            else
            {
                if (_currentWaypoint == null)
                {
                    GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

                    if (allWaypoints.Length > 0)
                    {
                        while (_currentWaypoint == null)
                        {
                            int random = UnityEngine.Random.Range(0, allWaypoints.Length);
                            ConnectedWaypoint startingWaypoint = allWaypoints[random].GetComponent <ConnectedWaypoint>();

                            if (startingWaypoint != null)
                            {
                                _currentWaypoint = startingWaypoint;
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Failed to find any waypoints for use in the scene");
                    }
                }

                SetDestination();
            }
        }