public bool CrossObstacle(Vector2 p1, Vector2 p2) { // Add a small threshold to the Rect float threshold = 0.2f; // Ray's direction is normalized... Vector2 dist = p2 - p1; float maxDist = dist.magnitude; Ray2D ray = new Ray2D(p1, dist); Rect obst = new Rect(); foreach (int i in obstacles) { /// Set obstacle obst.x = (i % imageSize.width) - threshold; obst.y = (i / imageSize.width) - threshold; obst.size = Vector2.one * (1 + threshold * 2); // Set line if (ray.Intersect(obst, maxDist)) { return(true); } } return(false); }