public New_Waypoint NextWaypoint(New_Waypoint previousWaypoint) { if (connections.Count == 0) //Revisa que la lista contenga objetos { Debug.LogError("Insufficient waypoint count"); //No hay waypoints, avisa de error y devuelve nulo return(null); } else if (connections.Count == 1 && connections.Contains(previousWaypoint)) //Revisa si la lista solo tiene 1 elemento y si es el mismo { Debug.LogWarning("Only one waypoint to move to..."); //Solo existe un waypoint, avisa de error y regresa el mismo return(previousWaypoint); } else //Ninguna de las anteriores, encuentra un waypoint aleatorio que no sea el pasado { New_Waypoint nextWaypoint; int nextIndex = 0; do { nextIndex = UnityEngine.Random.Range(0, connections.Count); nextWaypoint = connections[nextIndex]; } while (nextWaypoint == previousWaypoint); return(nextWaypoint); } }
List <New_Waypoint> connections; //Se declara la lista de new_waypoints public void Start() { GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Coloca todos los game objects con tag "Waypoint" en un array connections = new List <New_Waypoint>(); //Se inicializa la lista de new_waypoints for (int i = 0; i < allWaypoints.Length; i++) //Por cada objecto del array... { New_Waypoint nextWaypoint = allWaypoints[i].GetComponent <New_Waypoint>(); //Establece a nextWaypoint el componente new_waypoint del objeto del array if (nextWaypoint != null) //Revisa que contenga algo { if (Vector3.Distance(this.transform.position, nextWaypoint.transform.position) <= conectivityRad && nextWaypoint != this) //Revisa si la distancia entre este objecto y nextWaypoint es <= al radio de conectividad Y nextwaypoint no se si mismo { connections.Add(nextWaypoint); //Se agrega a la lista } } } }
private bool isPreparing; //Si esta en espera de cambiar de estado public void Start() { currentState = EnemyState.Patrol; //En un inicio el enemigo esta en el estado de patrulla navMeshAgent = this.GetComponent <NavMeshAgent>(); //Se asigna el componente "NavMeshAgent" a la variable if (navMeshAgent == null) //Revisa en caso que no tenga el componente { Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name); } else { if (currentWaypoint == null) //Revisa si la variable es nula { GameObject[] allWaypoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Guarda todos los objetos con tag "Waypoint" en un array if (allWaypoints.Length > 0) // Si el array contiene objetos { while (currentWaypoint == null) //Mientras la variable sea nula { int random = UnityEngine.Random.Range(0, allWaypoints.Length); //Variable local: Numero aleatorio entre 0 y longitud de array New_Waypoint startingWaypoint = allWaypoints[random].GetComponent <New_Waypoint>(); //Establece la variable startingWaypoint como un objeto random del array "allWaypoints" if (startingWaypoint != null) //Revisa si startingWaypoint no sea nula { currentWaypoint = startingWaypoint; //Obvio } } } else { Debug.LogError("Failed to find any waypoints for use in scene"); //Si no encuentra ningun objeto con el tag } } SetDestination(); //Ejecuta el metodo } currentState = EnemyState.Patrol; //Cambia el estado a patrullaje }
private void SetDestination() { if (waypointsVisited > 0) { New_Waypoint nextWaypoint = currentWaypoint.NextWaypoint(previousWaypoint); previousWaypoint = currentWaypoint; currentWaypoint = nextWaypoint; } Vector3 targetVector = currentWaypoint.transform.position; navMeshAgent.SetDestination(targetVector); isTraveling = true; currentState = EnemyState.Patrol; float rand = UnityEngine.Random.Range(0.0f, 1.0f); if (rand <= switchProb) { patrolWaiting = true; } }