The orthographicSize
camera is the number of units of world space in the upper half of the viewport. If it is 0.5, then 1 unit cube will precisely fill the viewing area (vertically).
To zoom in on your target area, point the camera at it (by setting (x, y) to the target center) and set orthographicSize
to half the height of the area.
Here is an example of centering and scaling the extents of an object. (Zoom in with LMB, "R" - reset.)
public class OrthographicZoom : MonoBehaviour { private Vector3 defaultCenter; private float defaultHeight; // height of orthographic viewport in world units private void Start() { defaultCenter = camera.transform.position; defaultHeight = 2f*camera.orthographicSize; } private void Update() { if (Input.GetMouseButtonDown(0)) { Collider target = GetTarget(); if(target != null) OrthoZoom(target.bounds.center, target.bounds.size.y); // Could directly set orthographicSize = bounds.extents.y } if (Input.GetKeyDown(KeyCode.R)) OrthoZoom(defaultCenter, defaultHeight); } private void OrthoZoom(Vector2 center, float regionHeight) { camera.transform.position = new Vector3(center.x, center.y, defaultCenter.z); camera.orthographicSize = regionHeight/2f; } private Collider GetTarget() { var hit = new RaycastHit(); Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit); return hit.collider; } }
source share