Pro_Unit

ZoomPan

Mar 8th, 2020
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. using UnityEngine;
  5.  
  6. public class ZoomPan : MonoBehaviour
  7. {
  8.     Vector3 touchStart;
  9.     public float zoomOutMin = 1;
  10.     public float zoomOutMax = 8;
  11.     public Camera cameraMap;
  12.  
  13.     // Update is called once per frame
  14.     void Update ()
  15.     {
  16.         if (Input.GetMouseButtonDown (0))
  17.         {
  18.             touchStart = cameraMap.ScreenToWorldPoint (Input.mousePosition);
  19.         }
  20.         if (Input.touchCount == 2)
  21.         {
  22.             Touch touchZero = Input.GetTouch (0);
  23.             Touch touchOne = Input.GetTouch (1);
  24.  
  25.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
  26.             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
  27.  
  28.             float prevMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
  29.             float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
  30.  
  31.             float difference = currentMagnitude - prevMagnitude;
  32.  
  33.             zoom (difference * 0.01f);
  34.         }
  35.         else if (Input.GetMouseButton (0))
  36.         {
  37.             Vector3 direction = touchStart - cameraMap.ScreenToWorldPoint (Input.mousePosition);
  38.             cameraMap.transform.position += direction;
  39.         }
  40.         zoom (Input.GetAxis ("Mouse ScrollWheel"));
  41.     }
  42.  
  43.     public void zoom (float increment)
  44.     {
  45.         cameraMap.orthographicSize = Mathf.Clamp (cameraMap.orthographicSize - increment, zoomOutMin, zoomOutMax);
  46.     }
  47. }
Add Comment
Please, Sign In to add comment