unity – 2D: Problem with mouse position and Physics2D.BoxCastAll

enter image description here

As you can see on this gif I have two issues:

  1. Spawning object based on mouse position looks wrong – it is not spawning where the mouse is pointing, but rather on the center of the screen (see this little yellow circle).

Code:
`
public class MoveToLocation : MonoBehaviour
{
public GameObject target;
private Camera _camera;

void Awake()
{
    _camera = Camera.main;
}

void Update()
{
    if (Input.GetMouseButtonDown(1) && target)
    {
        Destroy(GameObject.FindGameObjectWithTag("Target"));

        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 1.0f;
        Vector3 objectPosition = _camera.ScreenToViewportPoint(mousePosition);

        Instantiate(target, objectPosition, Quaternion.identity);
    }
}

}
`

  1. Using Physics2D.BoxCastAll based on rectangle is working fine when drawing is started on the top part of the screen, but when drawing is started on the bottom you can see this strange mirroring effect.

Code:

    void OnGUI()
{
    // LMB clicked
    if (Input.GetMouseButtonDown(0))
    {
        boxStartPosition = new Vector2(
            Input.mousePosition.x,
            Screen.height - Input.mousePosition.y
          );
    }

    // LMB drag
    if (Input.GetMouseButton(0))
    {
        boxEndPosition = new Vector2(
            Input.mousePosition.x,
            Screen.height - Input.mousePosition.y
        );

        rectangle = new Rect(
            boxStartPosition.x,
            boxStartPosition.y,
            boxEndPosition.x - boxStartPosition.x,
            boxEndPosition.y - boxStartPosition.y
        );

        GUI.DrawTexture(rectangle, RectangleTexture);

        float pixelsToWorld = Camera.main.orthographicSize / (Screen.height / 2f);

        Vector2 boxCenter = Camera.main.ScreenToWorldPoint(rectangle.center);

        boxCenter.y = Mathf.Abs(boxCenter.y);

        float boxWidth = Mathf.Abs(rectangle.size.x) * pixelsToWorld;
        float boxHeight = Mathf.Abs(rectangle.size.y) * pixelsToWorld;
        Vector2 boxSize = new Vector2(boxWidth, boxHeight);

        BoxCastDebug.DrawBoxCast2D(boxCenter, boxSize, 0f, Vector2.zero, 0f, Color.black);
        hits = Physics2D.BoxCastAll(boxCenter, boxSize, 0f, Vector2.zero, 0f);
    }

Can anyone help me to solve these issues? Is it directly connected with setup of my scene, camera or scripts?