using UnityEngine; namespace Google.Maps.Examples.Shared { /// /// This class implements a zoom for mobile which allows the user to control the field of view /// for a perspective camera or the orthographic size for an orthographic camera with two fingers. /// public class ZoomPinch : MonoBehaviour { #region properties /// /// The rate of change every frame of the field of view (angle) in perspective mode. /// [Tooltip("The rate of change of the field of view in perspective mode every frame.")] public float PerspectiveZoomSpeed = 0.5f; /// /// The rate of change every frame of the orthographic size (meters) in orthographic mode. /// [Tooltip("The rate of change of the orthographic size in orthographic mode.")] public float OrthoZoomSpeed = 0.5f; /// /// Reference to the active camera /// public Camera ActiveCamera; /// /// Min field of view /// [Tooltip("The minimum field of view when in perspective mode.")] public float MinFieldOfView = 30f; /// /// Max field of view /// [Tooltip("The maximum field of view when in perspective mode.")] public float MaxFieldOfView = 80; /// /// Max orthographic size (half height in meters). /// (Max zoom out) /// [Tooltip("The maximum size when in orthographic mode (Max zoom out).")] public float MaxOrthographicSize = 30f; /// /// Min orthographic size (half height in meters). /// (Max zoom in) /// [Tooltip("The minimum size when in orthographic mode (Max zoom in).")] public float MinOrthographicSize = 0.1f; #endregion /// /// Set the active camera to the main camera if it isn't already set. /// private void Start() { if (ActiveCamera == null) { ActiveCamera = Camera.main; } } /// /// Detects touches on screen. You need 2 touches to perform a zoom. /// Makes adjustments to camera orthographic size or camera field of view /// depending on camera settings. /// /// private void Update() { // If there are two touches on the device... if (Input.touchCount == 2) { // Store both touches. var touchZero = Input.GetTouch(0); var touchOne = Input.GetTouch(1); // Find the position in the previous frame of each touch. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // If the camera is orthographic... if (ActiveCamera.orthographic) { // ... change the orthographic size based on the change in distance between the touches. ActiveCamera.orthographicSize += deltaMagnitudeDiff * OrthoZoomSpeed; // Make sure the orthographic size is clamped. ActiveCamera.orthographicSize = Mathf.Clamp(ActiveCamera.orthographicSize, MinOrthographicSize, MaxOrthographicSize); } else { // Otherwise change the field of view based on the change in distance between the touches. ActiveCamera.fieldOfView += deltaMagnitudeDiff * PerspectiveZoomSpeed; // Clamp the field of view to make sure it's between min and max values. ActiveCamera.fieldOfView = Mathf.Clamp(ActiveCamera.fieldOfView, MinFieldOfView, MaxFieldOfView); } } } } }