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); } }
public 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(); } }
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; }
public void Start() { GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); _connections = new List <ConnectedWaypoint>(); 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); } } } }