Beispiel #1
0
        public MonsterConnectedWaypoints NextWaypoint(MonsterConnectedWaypoints previousWaypoint)
        {
            if (connections.Count == 0)
            {
                //No waypoints? Return null and complain.
                Debug.LogError("Insufficient waypoint count.");
                return(null);
            }
            else if (connections.Count == 1 && connections.Contains(previousWaypoint))
            {
                //Only one waypoint and it's the previous one? Just use that.
                return(previousWaypoint);
            }
            else
            {
                MonsterConnectedWaypoints 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)
            {
                MonsterConnectedWaypoints nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint);
                previousWaypoint = currentWaypoint;
                currentWaypoint  = nextWaypoint;
            }

            Vector3 targetVector = currentWaypoint.transform.position;

            navMeshAgent.SetDestination(targetVector);
            traveling = true;
        }
Beispiel #3
0
        // Start is called before the first frame update
        public void Start()
        {
            //Grab all waypoint objects in scene.
            GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");

            //Create a list of waypoints I can refer to later.
            connections = new List <MonsterConnectedWaypoints>();

            //Check if they're a connected waypoint.
            for (int i = 0; i < allWaypoints.Length; i++)
            {
                MonsterConnectedWaypoints nextWaypoint = allWaypoints[i].GetComponent <MonsterConnectedWaypoints>();

                //i.e. we found a waypoint.
                if (nextWaypoint != null)
                {
                    if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= connectivityRadius && nextWaypoint != this)
                    {
                        connections.Add(nextWaypoint);
                    }
                }
            }
        }
        // Start is called before the first frame update
        void Start()
        {
            navMeshAgent = GetComponent <NavMeshAgent>();

            if (navMeshAgent == null)
            {
                Debug.Log("The navmesh 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);
                            MonsterConnectedWaypoints startingWaypoint = allWaypoints[random].GetComponent <MonsterConnectedWaypoints>();

                            //i.e. we found a waypoint.
                            if (startingWaypoint != null)
                            {
                                currentWaypoint = startingWaypoint;
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Failed to find any waypoints for use in the scene.");
                    }
                }

                SetDestination();
            }
        }