Orthographic camera zoom towards transform

Hello guys. I’m having some issues programming the logic for zooming in towards a transforms.

I’ve tried to illustrate the scenario I’m having trouble with. But I’ll try to explain the issue in words as well. What I want to do is zoom the camera towards a chosen transform which is on-screen. However, when zooming the chosen transform should always remain at the same spot on the screen.

50614-zoom-in.png

This is what I’ve got so far

Camera.main.orthographicSize = zoomedValue;
Camera.main.transform.position = targetTransform.position;

This code zooms in on the chosen target, however, it also centers the target in the camera view. How would I make sure the transform remains at the same spot on the screen?

Your mistake lies in :

Camera.main.transform.position = targetTransform.position;

as it will put the camera on the same spot of the object therefore centering it. Try to move the position of the camera away or towards the object proportionally to the zoom you’re applying.