- Home /
How to add mimimum and maximum zoom effect?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Zoom : MonoBehaviour {
public float perspectiveZoomSpeed = 0.5f;
public float orthoZoomSpeed = 0.5f;
void Update() { if (Input.touchCount == 2) { Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
if (Camera.main.orthographic)
{
Camera.main.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize, 0.1f);
}
else
{
Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 0.1f, 179.9f);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612150335im_/https://answers.unity.com/themes/thub/images/avi.jpg)