I'm working on a 2D zoom effect using an orthographic camera. So far I have a zoom value that I'm using but I would like to know if there's a rule or formula to obtain the exact size for my orthographic camera in order to fit the object (target) exactly inside the camera rect.

Here's a sample of my code

var v = new Vector3(target.position.x, transform.position.y, target.position.z);
    mainCam.transform.position = Vector3.Lerp(mainCam.transform.position, v , smooth * Time.deltaTime);
    mainCam.orthographicSize = Mathf.Lerp(mainCam.orthographicSize, zoom ,smooth*Time.deltaTime);

I would like to know how to calculate the zoom variable instead of using a constant one.

I tried to upload the images directly but it failed so i'm providing a link, first image contains the object properties, second is a view of the object using a perspective camera (3d mode)and the third is what my camera sees when it's zoomed (the default orthographic size is 0.5 and the zoom size is equal to 0.1).

Thank you for your help.

  • \$\begingroup\$ Tell us more about the target object. To know how to measure its size, we need to know what it's built up out of. \$\endgroup\$ – DMGregory Aug 7 '18 at 11:35
  • \$\begingroup\$ the target is a 3d object (a building) but the camera is only facing it from the top, the y coordinate is constant and i'm only changing the x and z coordinates of the camera to zoom in on a specific building. (i hope this made things clearer) \$\endgroup\$ – Safa Aug 7 '18 at 11:47
  • \$\begingroup\$ When a user asks for clarification, edit your question to include the new info, so folks don't have to read the comment thread to understand your question. I'd recommend including an editor screenshot so we can see what makes up your building (is it a Mesh? Several parented meshes? Does it have colliders?) and your camera perspective. \$\endgroup\$ – DMGregory Aug 7 '18 at 11:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.