Instead of writing this:
if (!Input.GetMouseButtonDown(1)) { return; }
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
you could easily write:
if (!Mouse.current.rightButton.wasPressedThisFrame) { return; }
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());