private List <Waypoint> GetDirectNeighbours(Waypoint waypoint, List <Waypoint> edge) { List <Waypoint> neighbours = new List <Waypoint>(); int index = edge.IndexOf(waypoint); if (edge.Count > index + 1) { Waypoint neighbour = edge.ElementAt(index + 1); //get neighbours on same edge //if (pathfindingHelper.LineOfSight(neighbour,waypoint)) neighbours.Add(neighbour); } if (index > 0) { Waypoint neighbour = edge.ElementAt(index - 1); //get neighbours on same edge //if (pathfindingHelper.LineOfSight(neighbour, waypoint)) neighbours.Add(neighbour); } if (index == 0 || index == edge.Count) //if corner don't get any other neighbours { return(neighbours); } foreach (List <Waypoint> otherEdge in edges) //get neighbours on other edge { Waypoint closestWaypoint = null; float minDistance = float.MaxValue; Waypoint start = edge.ElementAt(0); Waypoint end = edge.Last(); Waypoint sharedCorner = Vector3.Distance(waypoint.globalPosition, start.globalPosition) < Vector3.Distance(waypoint.globalPosition, end.globalPosition) ? start : end; if (otherEdge != edge && otherEdge.Contains(sharedCorner)) //don't look on own edge and only look ad adjacent edges { foreach (Waypoint wp in otherEdge) { if (wp != waypoint /*&& wp!=start && wp!=end*/) //don't count yourself { float distance = Vector3.Distance(wp.globalPosition, waypoint.globalPosition); //measure distance of these waypoints if (minDistance > distance && pathfindingHelper.LineOfSight(wp, waypoint, false)) //if distance is smallest one, yet { minDistance = distance; closestWaypoint = wp; //define this waypoint as direct neighbour } } } } if (closestWaypoint != null && closestWaypoint != start && closestWaypoint != end) { neighbours.Add(closestWaypoint); } } return(neighbours); }
// Update is called once per frame void Update() { if (Vector3.Distance(nextPosition, transform.position) < 0.1) { if (corridor.Count > 0 /*&&stepsSkipted*/ && !corridorDone) { nextNode = corridor[0]; if (corridor.Count > 1) { lookaheadNode = corridor[1]; } else { lookaheadNode = corridor[0]; } nextPosition = corridor[0].publicGlobalPosition; //corridorStep++; corridor.RemoveAt(0); if (corridor.Count == 0) { corridorDone = true; } } else if (pathfindingHelper.LineOfSight(transform.position, destination.position, true)) { nextPosition = destination.position; corridorDone = true; } else { nextPosition = transform.position; corridorDone = false; } } movement.targetPoint = nextPosition; if (drawCorridor && !corridorDrawn) { DrawCorridor(); corridorDrawn = true; } }