Написал ответ сам
private void OnMouseDrag()
{
_newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
_newPos.x = Mathf.Clamp(_newPos.x, -2.73f, 2.84f);
_newPos.y = Mathf.Clamp(_newPos.y, -4.94f, 2.21f);
Collider2D hitCollider = Physics2D.OverlapPoint(_newPos);
if (CheckAccessMove())
{
if (hitCollider == null || hitCollider.gameObject == gameObject)
{
_newPos.x = Mathf.SmoothDamp(transform.position.x, _newPos.x, ref velocity.x, smoothTime, maxSpeed);
_newPos.y = Mathf.SmoothDamp(transform.position.y, _newPos.y, ref velocity.y, smoothTime, maxSpeed);
_rb.MovePosition(_newPos);
}
}
}
private bool CheckAccessMove()
{
RaycastHit2D[] hits = Physics2D.LinecastAll(new Vector2(transform.position.x, transform.position.y), _newPos);
foreach (RaycastHit2D hit in hits)
{
Collider2D collider = hit.collider;
if (collider.gameObject == gameObject)
continue;
if (collider != null)
return false;
}
return true;
}