camera zooming in unity
using UnityEngine;
using System.Collections;
public class MouseWheelZoom : MonoBehaviour {
float curZoomPos, zoomTo; // curZoomPos will be the value
float zoomFrom = 20f; //Midway point between nearest and farthest zoom values (a "starting position")
void Update ()
{
// Attaches the float y to scrollwheel up or down
float y = Input.mouseScrollDelta.y;
// If the wheel goes up it, decrement 5 from "zoomTo"
if (y >= 1)
{
zoomTo -= 5f;
Debug.Log ("Zoomed In");
}
// If the wheel goes down, increment 5 to "zoomTo"
else if (y >= -1) {
zoomTo += 5f;
Debug.Log ("Zoomed Out");
}
// creates a value to raise and lower the camera's field of view
curZoomPos = zoomFrom + zoomTo;
curZoomPos = Mathf.Clamp (curZoomPos, 5f, 35f);
// Stops "zoomTo" value at the nearest and farthest zoom value you desire
zoomTo = Mathf.Clamp (zoomTo, -15f, 30f);
// Makes the actual change to Field Of View
Camera.main.fieldOfView = curZoomPos;
}
//I SUGGEST YOU WATCH THE VIDEO ON HOW IT WORKS
//https://www.youtube.com/watch?v=0G4vcH9N0gc&t=127s
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PanZoom : MonoBehaviour {
Vector3 touchStart;
public float zoomOutMin = 1;
public float zoomOutMax = 8;
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0)){
touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
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 prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
float difference = currentMagnitude - prevMagnitude;
zoom(difference * 0.01f);
}else if(Input.GetMouseButton(0)){
Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
Camera.main.transform.position += direction;
}
zoom(Input.GetAxis("Mouse ScrollWheel"));
}
void zoom(float increment){
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment, zoomOutMin, zoomOutMax);
}
}