IEnumerator FollowIndoorPath() { Vector3 currentWaypoint = Vector3.zero; try { followingIndoorPath = true; currentWaypoint = path[0]; targetIndex = 0; } catch (IndexOutOfRangeException) { if (performingSequentialAction) { ManualDoNext(); } yield break; // Ends the coroutine; } while (true) { if (transform.position == currentWaypoint) { targetIndex++; if (targetIndex >= path.Length) { // End of the path reached followingIndoorPath = false; path = null; if (currentAction == StaffAction.leavingStore) { outside = true; GeneratePathToSidewalkEnd(); } if (currentAction == StaffAction.wandering) { print("Wandering location acheived"); } if (OnArrival != null) { OnArrival.Invoke(); } yield break; // Ends the coroutine } currentWaypoint = path[targetIndex]; } float staffSpeed = 0; staffSpeed = speed; currentWaypoint = new Vector3(currentWaypoint.x, .5f, currentWaypoint.z); transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, staffSpeed * Time.deltaTime); yield return(null); } }
IEnumerator FollowIndoorPath() { Vector3 currentWaypoint = Vector3.zero; try { followingIndoorPath = true; currentWaypoint = path[0]; targetIndex = 0; } catch (IndexOutOfRangeException) { yield break; // Ends the coroutine; } while (true) { // While loop runs constantly without freezing game because its inside a coroutine and yields each frame #region TestMovementMethod if (useRaycasting) { try { int forCount = 0; for (int i = targetIndex; i < path.Length; i++) { Vector3 dir = (transform.position - path[i]).normalized; float distance = Vector3.Distance(transform.position, path[i]); Ray ray = new Ray(path[i], dir); RaycastHit hit; Debug.DrawRay(ray.origin + new Vector3(0, .5f, 0), ray.direction * distance); if (!Physics.Raycast(ray.origin + new Vector3(0, .5f, 0), ray.direction * distance, out hit, distance)) // Raise the ray to y = .5 so the ray can hit obstacles { //print ("Not hitting anything (" + i + ")"); forCount++; continue; } else { if (hit.transform.gameObject.layer == 9) // 9 is the unwalkable layer { //print ("Hitting something when testing next waypoint! (" + i + ")" + "\n" + hit.transform.name); int temp = i - 1; currentWaypoint = path[temp]; targetIndex = temp; break; } } if (i == path.Length - 1) // Clear shot to target { int temp = path.Length; currentWaypoint = path [path.Length - 1]; targetIndex = path.Length - 1; } } } catch (IndexOutOfRangeException) { print("Following path using raycasting simplification method has failed"); } } #endregion // Tries to navigate paths by cutting corners if (transform.position == currentWaypoint) { targetIndex++; if (targetIndex >= path.Length) { // End of the path reached followingIndoorPath = false; path = null; if (currentAction == CustomerAction.leavingStore) { outside = true; leftStore = true; GeneratePathToSidewalkEnd(); } if (currentAction == CustomerAction.goingToComponent) { } if (OnArrival != null) { OnArrival.Invoke(); OnArrival = null; } yield break; // Ends the coroutine } currentWaypoint = path[targetIndex]; } float customerSpeed = 0; customerSpeed = (currentAction == CustomerAction.wandering) ? wanderingSpeed : speed; currentWaypoint = new Vector3(currentWaypoint.x, .5f, currentWaypoint.z); transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, customerSpeed * Time.deltaTime); yield return(null); } }