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 ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint) { if (_connections.Count == 0) { 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 //Otherwise, find a random one that isn't the previous one { 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; }
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); } } } }
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; if (_travelling) { n_animator.SetBool("walking", true); } }
// Use this for initialization public void Start() { n_animator = GetComponentInChildren <Animator>(); _navMeshAgent = this.GetComponent <NavMeshAgent>(); if (_navMeshAgent == null) { Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name); } else { if (_currentWaypoint == null) { //Set it at random. //Grab all waypoint objects in scene. 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>(); //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(); } }
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 <ConnectedWaypoint>(); //Check if they're a coonnected waypoint for (int i = 0; i < allWaypoints.Length; i++) { ConnectedWaypoint nextWaypoint = allWaypoints[i].GetComponent <ConnectedWaypoint>(); //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 = this.GetComponent <NavMeshAgent>(); if (navMeshAgent == null) { Debug.LogError("The nav mesh agent component is not attached to" + gameObject.name); } else { if (currentWaypoint == null) { //Set at random, grab all the waypoints in the scene 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>(); //found waypoint if (startingWaypoint != null) { currentWaypoint = startingWaypoint; } } } else { Debug.LogError("Failed to find any waypoints for use in the scene."); } } SetDestination(); } }