public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint) { //make sure there are connections if (_connections.Count == 0) { //if there are no connections complain Debug.LogError("Insufficient waypoints"); return(null); } else if (_connections.Count == 1 && _connections.Contains(previousWaypoint)) { //if there is only one connection and it is the previous one go back return(previousWaypoint); } else { //randomly pick a waypoint within range ConnectedWaypoint nextWaypoint; int nextIndex; do { nextIndex = UnityEngine.Random.Range(0, _connections.Count); nextWaypoint = _connections[nextIndex]; } while (nextWaypoint == previousWaypoint); return(nextWaypoint); } }
// Start is called before the first frame update void Start() { GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Make sure there are waypoints and they are tagged to create the list if (allWaypoints.Length == 0) { Debug.LogError("There are no objects with tag of Waypoint"); } _connections = new List <ConnectedWaypoint>(); //loop throught the waypoint list for (int i = 0; i < allWaypoints.Length; i++) { //Make sure that the object tagged waypoint actiually has a Connectedwaypoint object attached to it ConnectedWaypoint nextWaypoint = allWaypoints[i].GetComponent <ConnectedWaypoint>(); if (nextWaypoint != null) { //if the two objects are close enough add to _connections List if (nextWaypoint != this && Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= _connectivityRadius) { _connections.Add(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)) { return(previousWaypoint); } else { int nextIndex; var choices = connections.Where(waypoint => waypoint.GetInstanceID() != previousWaypoint.GetInstanceID()).ToList(); nextIndex = UnityEngine.Random.Range(0, choices.Count); /* * do * { * nextIndex = UnityEngine.Random.Range(0, _connections.Count); * nextWaypoint = _connections[nextIndex]; * } while (nextWaypoint == previousWaypoint); */ return(choices[nextIndex]); } }
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(ConnectedWaypoint destination) { if (_navMeshAgent != null && destination != null) { _navMeshAgent.SetDestination(destination.transform.position); } }
public ConnectedWaypoint NextWaypoint(ConnectedWaypoint 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 //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); } }
void Patrol() { agent.speed = patrolSpeed; waitingAgent = false; if (agent == null) { Debug.LogError("Nav mesh agent component not attached to: " + gameObject.name); } else { if (_currentWaypoint == null) //setting initial waypoint { GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); while (_currentWaypoint == null) { int random = UnityEngine.Random.Range(0, allWaypoints.Length); ConnectedWaypoint initialWaypoint = allWaypoints[random].GetComponent <ConnectedWaypoint>(); _currentWaypoint = initialWaypoint; } } if (!travelling) { SetDestination(); } } }
public ConnectedWaypoint nextWaypoint(ConnectedWaypoint previousWaypoint) { if (connections.Count == 0) { //returns null when no waypoints are in range idle = true; Debug.LogError("no waypoint found"); return(null); } //only 1 waypoint else if (connections.Count == 1 && connections.Contains(previousWaypoint)) { idle = false; return(previousWaypoint); } //random waypoint that is not the previos one else { idle = false; ConnectedWaypoint nextWaypoint; int nextIndex = 0; do { nextIndex = UnityEngine.Random.Range(0, connections.Count); nextWaypoint = connections[nextIndex]; } while (nextWaypoint == previousWaypoint); return(nextWaypoint); } }
public void Start() { CheckSector(); check = false; beingVisited = false; weightWaypoint = 0; allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); for (int i = 0; i < allWaypoints.Length; i++) //creating list of connected weighpoints { nextWaypoint = allWaypoints[i].GetComponent <ConnectedWaypoint>(); if (nextWaypoint != this && nextWaypoint != null) { //if((Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= _connectivityRadius)) //if near any other waypoints //{ // _connections.Add(nextWaypoint, 0); //add to dictionary with weight of 0. //} str2 = nextWaypoint.GetComponent <ConnectedWaypoint>()._sector; if (str2.Contains(_sector)) { _connections.Add(nextWaypoint, 0); } } } List <ConnectedWaypoint> keys = new List <ConnectedWaypoint>(_connections.Keys); foreach (ConnectedWaypoint item in keys) { _keys.Add(item); } }
public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint) { if (_connections.Count == 0) { Debug.LogError("No waypoints!!"); return(null); } else if (_connections.Count == 1 && _connections.Contains(previousWaypoint)) { // Only 1 and its previous, just do it!!! return(previousWaypoint); } else // Otherwise, find a random one that is different from the previous { ConnectedWaypoint nextWaypoint; int nextIndex = 0; do { nextIndex = Random.Range(0, _connections.Count); nextWaypoint = _connections[nextIndex]; } while (nextWaypoint == previousWaypoint); return(nextWaypoint); } }
public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint) { if (_connections.Count == 0) { //no hi ha waypoints, retorna null Debug.LogError("Insuficient waypoint count."); return(null); } else if (_connections.Count == 1 && _connections.Contains(previousWaypoint)) { //nomes un waypoint, usa aquest return(previousWaypoint); } else { ConnectedWaypoint nextWaypoint; int nextIndex = 0; do { nextIndex = UnityEngine.Random.Range(0, _connections.Count); nextWaypoint = _connections [nextIndex]; } while(nextWaypoint == previousWaypoint); return(nextWaypoint); } }
// Start is called before the first frame update void Start() { _navMeshAgent = this.GetComponent <NavMeshAgent>(); if (_navMeshAgent == null) { Debug.Log("NavMesh Not Found"); } 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 waypoint"); } } curState = States.Patrol; Patrolling(); }
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) { // No waypoint Debug.LogError("Insuffcient waypoint count"); return(null); } else if (connections.Count == 1 && connections.Contains(previousWaypoint)) { // Only one waypoint and it's the previous one 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 Start() { _navMeshAgent = this.GetComponent <NavMeshAgent>(); if (_navMeshAgent == null) { Debug.LogError($"The navmesh agent 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 startingPoint = allWaypoints[random].GetComponent <ConnectedWaypoint>(); if (startingPoint != null) { _currentWaypoint = startingPoint; _previousWaypoint = _currentWaypoint; } } } /* * if (allWaypoints.Length == 0) { Debug.LogError("No enough Waypoints"); } * * int random = UnityEngine.Random.Range(0, allWaypoints.Length); * _currentWaypoint = allWaypoints[random].GetComponent<ConnectedWaypoint>(); */ } else { Debug.LogError($"Failed to find waypoints in the scene"); } } SetDestination(); }
public void SetDestination() { if (waypointsVisited > 0) { if (_previousWaypoint == null) { _previousWaypoint = _currentWaypoint; } ConnectedWaypoint nextWaypoint = _currentWaypoint.NextWaypoint(_previousWaypoint); //gets the next waypoint from the ConnectedWaypoint _previousWaypoint = _currentWaypoint; _currentWaypoint = nextWaypoint; } Vector3 targetVector = _currentWaypoint.transform.position; myWaypoint = _currentWaypoint; myWaypoint.GetComponent <ConnectedWaypoint>().WeightWaypoint(); //update visiting and weight of waypoint agent.SetDestination(targetVector); travelling = true; }
// Start is called before the first frame update void Start() { anim = GetComponent <Animator>(); _navMeshAgent = this.GetComponent <NavMeshAgent>(); //_navMeshAgent.updateRotation = false; if (_navMeshAgent == null) { Debug.LogError("the nav mesh agent component is not attatched to " + gameObject.name); } else { if (currentWaypoint == null) { //set to random and get other waypoints in scene GameObject[] allwaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); if (allwaypoints.Length > 0) { while (currentWaypoint == null) { int random = UnityEngine.Random.Range(0, allwaypoints.Length); ConnectedWaypoint startingPoint = allwaypoints[random].GetComponent <ConnectedWaypoint>(); //found waypoint if (startingPoint != null) { currentWaypoint = startingPoint; } } } else { Debug.LogError("cannot find waypoints to use in this scene"); } } SetDestination(); } }
public void ResetDestination() { if (waypointsVisited > 0) { ConnectedWaypoint nextWaypoint = currentWaypoint.nextWaypoint(previousWaypoint); previousWaypoint = currentWaypoint; currentWaypoint = nextWaypoint; } Vector3 target = currentWaypoint.transform.position; _navMeshAgent.SetDestination(target); _traveling = true; if (currentWaypoint.name == "PlayerPoint") { _navMeshAgent.isStopped = true; gameObject.GetComponent <EnemyMove>(); Debug.Log("hello"); } }
// Start is called before the first frame update void Start() { //find everywaypoint in the scene GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); connections = new List <ConnectedWaypoint>();//list all the points //check if waypoints are connected 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 Awake() { _waitTimer = 0; _waypointsVisited = 0; _navMeshAgent = this.GetComponent <NavMeshAgent>(); this.anim = this.gameObject.GetComponent <Animator>(); if (_navMeshAgent == null) { Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name); } else { if (_currentWaypoint == null) { 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; //_travelling = true; } // } } else { Debug.LogError("Failed to find any waypoints for use in the scene"); } } rand = Random.Range(0, 2); } }
public void Start() { //Grab all waypoints in the scene GameObject[] allWayPoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Create a list of waypoints _connections = new List <ConnectedWaypoint>(); //check if the waypoints are connected 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); } } } }
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 connected 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); } } } }
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 //currently finds a random waypoint,maybe add differnt logic here { ConnectedWaypoint nextWaypoint; int nextIndex = 0; do { nextIndex = Random.Range(0, connections.Count); nextWaypoint = connections[nextIndex]; } while (nextWaypoint == previousWaypoint); return(nextWaypoint); } }
public ConnectedWaypoint NextWaypoint(ConnectedWaypoint previousWaypoint) { if (_weightedConnections.Count == 0) { Debug.LogError("No nearby waypoints found!"); return(null); } else if (_weightedConnections.Count == 1 && _weightedConnections.Contains(previousWaypoint)) { return(previousWaypoint); } else { ConnectedWaypoint nextWaypoint = previousWaypoint; //if cannot find one to visit, return to previous waypoint - only time two AI will visit same spot. int nextWaypointIndex = 0; if (_weightedConnections.Count > 0) { nextWaypointIndex = UnityEngine.Random.Range(0, _weightedConnections.Count); nextWaypoint = _weightedConnections[nextWaypointIndex]; } return(nextWaypoint); } }