public void FixedUpdate() { if (target == null || target.dead) { Destroy(gameObject); } else { if (Vector2.Distance(target.transform.position, transform.position) < target.radius) { hit(target); Destroy(gameObject); } // It looks weird to have an arrow 'lead' a target, so I seek seekBehaviour.setTarget(target.transform.position); } }
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); } }