Camera zoom while keeping an off-center object at the same screen co-ordinates

Camera zoom whiloe keeping an offcenter object at the same screen co-ordinates.

Im doing a zoom by dragging on the highligted red ball below.

How do I get the ball to stay at it’s screen coords while the rest of the view zooms in?

Thanks in advance


I’ve not got time right now to write the test code, but here is the idea:

  • Use Camera.main.WorldToViewportPoint() before making the move (or even in Start()) to get the position in viewport space of the object.
  • Move the camera in for the zoom.
  • Get the new Viewport coordinates for the new position of the object.
  • Using the new distance between the camera and the board, calculate the world positions for the two viewport positions using Camera.ViewportToWorldPoint();
  • Subtract the two to get how far to translate the board or the camera.