private void Update() { // Do the current state of this enemy. switch (this.state) { case EnemyControllerState._idle: // Check if been idle long enough if (Time.time >= this.exitIdleTime) { this.state = EnemyControllerState._waypoints; break; } // Shouldn't have a target object if not chasing. this.targetObject = null; // Shouldn't have a path if idle. this.navMeshAgent.ResetPath(); break; case EnemyControllerState._waypoints: // Set a new random destination if not already heading to one. if (this.navMeshAgent.hasPath == false) { // Clean out any empty waypoint elements. this.waypoints.RemoveAll(item => item == null); if (this.waypoints.Count > 0) { this.navMeshAgent.SetDestination(this.waypoints[Random.Range(0, this.waypoints.Count)].position); } else { this.state = EnemyControllerState._idle; this.exitIdleTime = Time.time + this.idleWaitTime; } } // Shouldn't have a target object if not chasing. this.targetObject = null; break; case EnemyControllerState._chase: // Check still got a target object or set back to idle. if (this.targetObject == null) { this.state = EnemyControllerState._idle; break; } // Set navMeshAgent destination to target object position. this.navMeshAgent.SetDestination(this.targetObject.transform.position); break; } }
public override void SetTarget(GameObject targetGameObject) { // Set the target object. this.targetObject = targetGameObject; // Check if there is a target object. if (this.targetObject == null) { if (this.state == EnemyControllerState._chase) { this.state = EnemyControllerState._idle; } // Stop chasing if no target. return; } // Start chasing. this.state = EnemyControllerState._chase; }