juls0730

Camera

Oct 1st, 2021
1,514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PanZoom : MonoBehaviour    {
  6.         Vector3 touchStart;
  7.         public float ZoomOutMin = 1;
  8.         public float ZoomOutMax = 6;
  9.         public float upBound = 14f;
  10.         public float downBound = -14f;
  11.         public float leftBound = -20f;
  12.         public float rightBound = 20f;
  13.        
  14.     void Update()   {
  15.         if(Input.GetMouseButtonDown(0)){
  16.          if (Camera.main.transform.position.x <= leftBound) {
  17.                 Camera.main.transform.position = new Vector2(leftBound, transform.position.y);
  18.                 } else if (Camera.main.transform.position.x >= rightBound) {
  19.                 Camera.main.transform.position = new Vector2(rightBound, transform.position.y);
  20.                 } else  if (transform.position.y <= downBound) {
  21.                 transform.position = new Vector2(transform.position.x, downBound);
  22.                 } else if (transform.position.y >= upBound) {
  23.                 transform.position = new Vector2(transform.position.x, upBound);
  24.                 } else {
  25.                 touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  26.             }
  27.         }
  28.         if(Input.touchCount == 2){
  29.             Touch touchZero = Input.GetTouch(0);
  30.             Touch touchOne = Input.GetTouch(1);
  31.  
  32.             Vector2 touchZeroPrePos = touchZero.position - touchZero.deltaPosition;
  33.             Vector2 touchOnePrePos = touchOne.position - touchOne.deltaPosition;
  34.  
  35.             float preMagnitude = (touchZeroPrePos - touchOnePrePos).magnitude;
  36.             float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
  37.  
  38.             float diff = currentMagnitude - preMagnitude;
  39.  
  40.             zoom(diff * 0.01f);
  41.         }else if(Input.GetMouseButton(0)){
  42.             Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
  43.             Camera.main.transform.position += direction;
  44.         }
  45.         zoom(Input.GetAxis("Mouse ScrollWheel"));
  46.     }
  47.    
  48.     void zoom(float inc) {
  49.         Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - inc, ZoomOutMin, ZoomOutMax);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment