private void NavigateACOToStart() { if (m_acoToStartPath != null && m_acoToStartPath.Count > 0 && m_acoToStartNavInfo != null) { NavigateGeneric(m_acoToStartNavInfo.TargetNode); /// Check if agent reached next route node float nextNodeDistance = Vector3.Distance(transform.position, m_acoToStartNavInfo.TargetNode.transform.position); if (nextNodeDistance < DESTINATION_TOLERANCE) { /// Increment target index m_acoToStartNavInfo.TargetIndex += 1; /// Check if index is more or equal to path length if (m_acoToStartNavInfo.TargetIndex >= m_acoToStartPath.Count) { /// Finished all paths, reset and sleep m_currentDrivePathTarget = NavigationTarget.None; m_ui.SetStatusText("Finished and returned home. Sleeping (zzz)"); Debug.Log($"Agent '{this.gameObject.name}' finished ACO path, duration of '{m_totalDuration}s'"); ResetPath(); } else { /// Set next to node if inside range Connection nextConn = m_acoToStartPath[m_acoToStartNavInfo.TargetIndex]; OnTravelNewConnection?.Invoke(this, nextConn); m_acoToStartNavInfo.TargetNode = nextConn.ToNode; } } } }
private void NavigateStartToACO() { if (m_startToACOPath != null && m_startToACOPath.Count > 0 && m_startToACONavInfo != null) { NavigateGeneric(m_startToACONavInfo.TargetNode); /// Check if agent reached next route node float nextNodeDistance = Vector3.Distance(transform.position, m_startToACONavInfo.TargetNode.transform.position); if (nextNodeDistance < DESTINATION_TOLERANCE) { /// Increment target index m_startToACONavInfo.TargetIndex += 1; /// Check if index is more or equal to path length if (m_startToACONavInfo.TargetIndex >= m_startToACOPath.Count) { /// Set drive path to ACO, the next path m_currentDrivePathTarget = NavigationTarget.ACO; Debug.Log($"Agent '{this.gameObject.name}' completed A* Navigation. Proceeding with ACO path..."); } else { /// Set next to node if inside range Connection nextConn = m_startToACOPath[m_startToACONavInfo.TargetIndex]; OnTravelNewConnection?.Invoke(this, nextConn); m_startToACONavInfo.TargetNode = nextConn.ToNode; } } } }
/// <summary> /// Drives the truck along a certain connection path /// </summary> /// <param name="connectionPath">List of connections to drive along</param> public void DriveAlong(List <Connection> connectionPath) { // Reset any previous paths m_connectionDrivePath = null; m_currentTargetNode = null; if (connectionPath == null || connectionPath != null && connectionPath.Count <= 0) { Debug.LogError("Unable to drive along connection path. connectionPath invalid"); return; } m_connectionDrivePath = connectionPath; Debug.Log($"Truck '{this.name}' path set! '{m_connectionDrivePath.Count}' connections"); if (m_currentTargetNode == null) { // Set Truck position from first FromNode and target to ToNode this.transform.position = m_connectionDrivePath[m_currentTargetNodeIndex].FromNode.transform.position; Connection nextConnection = m_connectionDrivePath[m_currentTargetNodeIndex]; m_currentTargetNode = nextConnection.ToNode; // Fire event to first set new target location OnTravelNewConnection?.Invoke(this, nextConnection); m_ui.SetStatusText($"New Path: Driving to '{connectionPath[connectionPath.Count - 1].ToNode.name}'"); } }
void Update() { if (m_currentTargetNode) { // Do target node look at if target is valid PerformLookAt(m_currentTargetNode.transform); m_totalDuration += Time.deltaTime; if (!IsWaiting) { // Move Truck towards next connection PerformMovement(); // If nearly reached destination float nextNodeDistance = Vector3.Distance(transform.position, m_currentTargetNode.transform.position); if (nextNodeDistance < DESTINATION_TOLERANCE) { // Increment and set new target game object m_currentTargetNodeIndex++; // Move target node to next node index if (m_currentTargetNodeIndex > 0 && m_currentTargetNodeIndex < m_connectionDrivePath.Count) { Connection currentTargetConnection = m_connectionDrivePath[m_currentTargetNodeIndex]; m_currentTargetNode = currentTargetConnection.ToNode; OnTravelNewConnection?.Invoke(this, currentTargetConnection); } else { //Debug.LogError("TargetNodeIndex is out of bounds!"); } } // Check if reached final node yet float finalNodeDistance = Vector3.Distance(transform.position, m_connectionDrivePath[m_connectionDrivePath.Count - 1].ToNode.transform.position); if (finalNodeDistance < DESTINATION_TOLERANCE) { // Invoke event for reached path end and remove target Connection finalConnection = m_connectionDrivePath[m_connectionDrivePath.Count - 1]; OnReachedPathEnd?.Invoke(this, finalConnection.ToNode); string infoStr = $"Finished path to '{finalConnection.ToNode.name}', duration of '{Math.Round(m_totalDuration, 2)}s'"; m_ui.SetStatusText(infoStr); Debug.Log($"Agent '{this.gameObject.name}' " + infoStr); ResetPath(); } } } }
private void NavigateACO() { if (m_currentTargetACOConn != null) { /// Look at next target node GameObject targetNodeObj = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex].ToNode; PerformLookAt(targetNodeObj.transform); if (!IsWaiting) { /// if not waiting, move toward next route node PerformMovementTo(targetNodeObj.transform.position); /// Check if agent reached next route node float nextNodeDistance = Vector3.Distance(transform.position, targetNodeObj.transform.position); if (nextNodeDistance < DESTINATION_TOLERANCE) { /// Reached next route node, increment to next route node or to new ACOConnection m_currentACOConnRouteIndex++; /// Check RouteIndex is within route bounds if (m_currentACOConnRouteIndex >= m_currentTargetACOConn.Route.Count) { /// If index is more than route, we've reached end and can move to next ACOConnection m_currentACOConnRouteIndex = 0; m_currentTargetACOConnIndex++; /// Check if reached end of ACOConnection path if (m_currentTargetACOConnIndex >= m_acoConnectionPath.Count) { ACOConnection finalConnection = m_acoConnectionPath[m_acoConnectionPath.Count - 1]; OnReachedPathEnd?.Invoke(this, finalConnection.ToNode); m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'"); m_currentDrivePathTarget = NavigationTarget.ACOToStart; Debug.Log($"Agent '{this.name}' finished ACO path. Navigating A* path to Start"); return; } else { /// Move to next node in Route //Debug.Log($"Reached ACO goal {m_currentTargetACOConn.ToNode.name}"); OnReachedGoal?.Invoke(this, m_currentTargetACOConn.ToNode); /// Continue moving through ACOConnection path if not at end m_currentTargetACOConn = m_acoConnectionPath[m_currentTargetACOConnIndex]; OnTravelNewConnection?.Invoke(this, m_currentTargetACOConn); return; } } else { /// Still more Route nodes to travel to, invoke event to specify the connection Connection nextRouteConnection = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex]; OnTravelNewConnection?.Invoke(this, nextRouteConnection); } } } } }
void Update() { if (m_currentTargetACOConn != null) { GameObject targetNodeObj = m_currentTargetACOConn.Route[m_currentACOConnRouteIndex].ToNode; /// Look at next target node PerformLookAt(targetNodeObj.transform); if (!IsWaiting) { /// if not waiting, move toward next route node PerformMovementTo(targetNodeObj.transform.position); /// Check if agent reached next route node float nextNodeDistance = Vector3.Distance(transform.position, targetNodeObj.transform.position); if (nextNodeDistance < DESTINATION_TOLERANCE) { /// Reached next route node, increment to next route node or to new ACOConnection m_currentACOConnRouteIndex++; if (m_currentACOConnRouteIndex >= m_currentTargetACOConn.Route.Count) { m_currentACOConnRouteIndex = 0; m_currentTargetACOConnIndex++; /// Check if reached end of ACOConnection path if (m_currentTargetACOConnIndex >= m_connectionPath.Count) { ACOConnection finalConnection = m_connectionPath[m_connectionPath.Count - 1]; OnReachedPathEnd?.Invoke(this, finalConnection.ToNode); m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'"); ResetPath(); return; } else { /// Continue moving through ACOConnection path if not at end m_currentTargetACOConn = m_connectionPath[m_currentTargetACOConnIndex]; OnTravelNewConnection?.Invoke(this, m_currentTargetACOConn); return; } } } // Check if agent reached final ACOConnection node float finalNodeDistance = Vector3.Distance(transform.position, m_connectionPath[m_connectionPath.Count - 1].ToNode.transform.position); if (finalNodeDistance < DESTINATION_TOLERANCE && m_currentTargetACOConnIndex >= m_connectionPath.Count) { // Invoke event for reached path end and remove target ACOConnection finalConnection = m_connectionPath[m_connectionPath.Count - 1]; OnReachedPathEnd?.Invoke(this, finalConnection.ToNode); m_ui.SetStatusText($"Finished path to '{finalConnection.ToNode.name}'"); ResetPath(); } } } }