void FixedUpdate() { Vector2 nextPoint = wayPoints[nextWayPointIndex]; arrivalBehaviour.setTarget(nextPoint); if ((nextPoint - (Vector2)transform.position).sqrMagnitude < 0.1f) { nextWayPointIndex = (nextWayPointIndex + 1) % wayPoints.Count; } }
public void followPath(Steering steering, Seek seekBehaviour, Arrival arrivalBehaviour, Brake brakeBehaviour) { Vector2 next = nextPoint(); float nextRadius = (points.Count == 1) ? destRadius : steering.getSize(); bool arrivedAtPoint = (next - steering.getPosition()).sqrMagnitude < nextRadius * nextRadius; arrived = arrivedAtPoint && points.Count == 1; if (arrivedAtPoint && points.Count > 1) { points.RemoveAt(0); // accelerate toward the next node followPath(steering, seekBehaviour, arrivalBehaviour, brakeBehaviour); } else { if (Pathing.raycastGameCoordinates(steering.getPosition(), steering.getSize(), next, LayerMask.GetMask("Walls"))) { // A wall is in the way. Check if the raycast would be fine from the center of the current tile. Vector2 currentTileCenter = Pathing.map.mapToGame(Pathing.map.gameToMap(steering.getPosition())); if (Pathing.raycastGameCoordinates(currentTileCenter, steering.getSize(), next, LayerMask.GetMask("Walls"))) { // The unit must have detoured and there are now walls in the way, calculate a new path. points = Pathing.findPath(steering.getPosition(), goal, destRadius, steering.getSize()).points; } else { // The unit is stuck because they are a little bit off-center. points.Insert(0, currentTileCenter); } } // Stop after arriving. steering.updateWeight(brakeBehaviour, arrived ? 1f : 0f); // Arrival for the last point, seek for every other point arrivalBehaviour.setTarget(next); seekBehaviour.setTarget(next); steering.updateWeight(arrivalBehaviour, points.Count == 1 ? 1f : 0f); steering.updateWeight(seekBehaviour, points.Count > 1 ? 1f : 0f); } }