Camera spelling with focus on a specific point

I have a spelling camera and you want to implement the zoom function for a specific point. Suppose you have an image and you want to enlarge it to a certain part of the image.

I know how to zoom in, the problem is to move the camera to a position in which there is a desired area in focus.

How can i do this?

+4
source share
2 answers

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; } } 
0
source

hope this code example is useful, should be copied / pasted. Note: this script assumes that it is attached to the camera object, otherwise you must configure the conversion to the camera object link.

 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; } } 
0
source

Source: https://habr.com/ru/post/1500125/


All Articles