Orthographic camera zoom with focus on a specific point
Asked Answered
C

2

6

I have an orthographic camera and would like to implement a zoom feature to a specific point. I.e., imagine you have a picture and want to zoom to a specific part of the picture.

I know how to zoom in, the problem is to move the camera to a position that has the desired zone in focus.

How can I do so?

Cornered answered 2/9, 2013 at 11:7 Comment(0)
T
0

The camera's orthographicSize is the number of world space units in the top half of the viewport. If it's 0.5, then a 1 unit cube will exactly fill the viewport (vertically).

So to zoom in on your target region, center your camera on it (by setting (x,y) to the target's center) and set orthographicSize to half the region's height.

Here is an example to center and zoom to the extents of an object. (Zoom with LMB; 'R' to 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;
    }
}
Theocritus answered 2/9, 2013 at 20:40 Comment(0)
C
0

hope you find this code example useful, should be a copy / paste. Note: this script assume it's attached to the camera object, otherwise you should adjust the transform to the camera object reference.

private float lastZoomDistance = float.PositiveInfinity; // remember that this should   be reset to infinite on touch end
private float maxZoomOut = 200;
private float maxZoomIn = 50;
private float zoomSpeed = 2;

void Update() {

if (Input.touchCount >= 2) {

    Vector2 touch0, touch1;
    float distance;
    Vector2 pos = new Vector2(transform.position.x, transform.position.y);
    touch0 = Input.GetTouch(0).position - pos;
    touch1 = Input.GetTouch(1).position - pos;

    zoomCenter = (touch0 + touch1) / 2;

    distance = Vector2.Distance(touch0, touch1);
    if(lastZoomDistance == float.PositiveInfinity) {
        lastZoomDistance = distance;
    } else {
        if(distance > lastZoomDistance && camera.orthographicSize + zoomSpeed <= maxZoomOut) {
            this.camera.orthographicSize = this.camera.orthographicSize + zoomSpeed;
            // Assuming script is attached to camera - otherwise, change the transform.position to the camera object
            transform.position = Vector3.Lerp(transform.position, zoomCenter, Time.deltaTime);
        } else if(distance < lastZoomDistance && camera.orthographicSize - zoomSpeed >= maxZoomIn) {
            this.camera.orthographicSize = this.camera.orthographicSize - zoomSpeed;
            transform.position = Vector3.Lerp(transform.position, zoomCenter, Time.deltaTime);
        }
    }
    lastZoomDistance = distance;
}

}
Catchweight answered 17/6, 2014 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.