void Patrol() { //Have the character move to a random way point based on errors. agent.speed = patrolSpeed; //Set sound //noise.setCurrentDecibel(this); //this.GetComponent<SphereCollider>().radius = noise.currentDecibel; //If player is within the range of a random way point, go to it. if (Vector3.Distance(this.transform.position, currentWaypoint.transform.position) >= 2) { agent.SetDestination(currentWaypoint.transform.position); character.Move(agent.desiredVelocity, false, false); } //If the player is close to way point, set the next way point. else if (Vector3.Distance(this.transform.position, currentWaypoint.transform.position) <= 2) { currentWaypoint = sn.NextWayPoint(currentWaypoint); } //If there are no way points close by. else { character.Move(Vector3.zero, false, false); } }
// Use this for initialization void Start() { agent = GetComponent <NavMeshAgent>(); character = GetComponent <ThirdPersonCharacter>(); agent.updatePosition = true; agent.updateRotation = false; state = bev_basicAI.State.PATROL; alive = true; sn = this.GetComponent <WayPointMaster>(); noise = this.GetComponent <DecibelTracker>(); //Patroling //Tracks visited way points. currentWaypoint = sn.NewWayPoint(); //Get a random way point //GameObject[] tempPoints = GameObject.FindGameObjectsWithTag("Waypoint"); //currentWaypoint = tempPoints[0].GetComponent<WayPointClass>(); //waypoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Start FSM Finite state machine StartCoroutine("FSM"); }
public WayPointClass NextWayPoint(WayPointClass previousWaypoint) { //Set waypoint visit int currentIndex = waypoints.IndexOf(previousWaypoint); waypoints[currentIndex].resetVisit(); //Return way point WayPointClass nextWaypoint; //Get only visited nodes notVisited = waypoints.Where(x => x.visited == false).ToList(); //Waypoints int waypointIndex = UnityEngine.Random.Range(0, notVisited.Count); if (notVisited.Count == 0) { //Reset all waypoints for (int i = 0; i < waypoints.Count; i++) { waypoints[i].resetVisit(); } //Reset for new visits notVisited = waypoints.Where(x => x.visited == false).ToList(); waypointIndex = UnityEngine.Random.Range(0, notVisited.Count); } //Get an index nextWaypoint = notVisited[waypointIndex]; return(nextWaypoint); }
void Awake() { //Get all way points for the player. waypoints = new List <WayPointClass>(); GameObject[] tempPoints = GameObject.FindGameObjectsWithTag("Waypoint"); //Debug.LogError("Faild to find waypoints"+ tempPoints.Length); for (int i = 0; i < tempPoints.Length; i++) { WayPointClass temp = tempPoints[i].GetComponent <WayPointClass>(); waypoints.Add(temp); } }