private bool CastLine(Line line, Vector2 direction, float distance, Vector2 offset, float padding, ref RaycastHit2D closestHit, DebugData.DebugTag tag) { GetLinePoints(line, out var start, out var end); return(CastLine(start + offset, end + offset, direction, distance, padding, ref closestHit, tag)); }
private bool CastLine(Vector2 start, Vector2 end, Vector2 direction, float distance, float padding, ref RaycastHit2D closestHit, DebugData.DebugTag tag) { float closestHitDistance = distance + padding; bool hasHit = false; for (int i = 0; i < pointsPerLine; i++) { float ratio = i / (float)(pointsPerLine - 1); Vector2 position = Vector2.Lerp(start, end, ratio); RaycastHit2D[] hits = Physics2D.RaycastAll(position, direction, closestHitDistance, layerMask); foreach (var hit in hits) { // Ignore things we are moving away from. if (Vector2.Dot(hit.normal, direction) > Mathf.Epsilon) { continue; } if (hit.distance < closestHitDistance) { closestHitDistance = hit.distance; closestHit = hit; closestHit.distance = Mathf.Max(0.0f, closestHit.distance - padding); hasHit = true; } } data.Insert(tag, new DebugData.RaycastInfo() { start = position, end = position + direction * (hasHit ? closestHit.distance : closestHitDistance), }); } return(hasHit); }
private bool CastLine(Line line, Vector2 direction, float distance, ref RaycastHit2D closestHit, DebugData.DebugTag tag) { return(CastLine(line, direction, distance, Vector2.zero, skinwidth, ref closestHit, tag)); }