public RaycastHit2D GetHorizontalCollision(Vector3 direction) { direction = Vector3.right * direction.x; var offset = (_collider.bounds.size.y - VERTICAL_OVERFLOW.y) / RAYCAST_GRANULARITY; var raycastOrigin = _collider.Bottom() + VERTICAL_OVERFLOW; var raycastDistance = _collider.bounds.extents.x + COLLIDER_SIZE_OVERFLOW; for (int i = 0; i < RAYCAST_GRANULARITY; i++) { var raycast = Physics2D.Raycast(raycastOrigin + (Vector3.up * offset * i), direction, raycastDistance, _groundLayer); if (raycast.collider != null) { return(raycast); } } return(new RaycastHit2D()); }