Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PanZoom : MonoBehaviour {
- Vector3 touchStart;
- public float ZoomOutMin = 1;
- public float ZoomOutMax = 6;
- public float upBound = 14f;
- public float downBound = -14f;
- public float leftBound = -20f;
- public float rightBound = 20f;
- void Update() {
- if(Input.GetMouseButtonDown(0)){
- if (Camera.main.transform.position.x <= leftBound) {
- Camera.main.transform.position = new Vector2(leftBound, transform.position.y);
- } else if (Camera.main.transform.position.x >= rightBound) {
- Camera.main.transform.position = new Vector2(rightBound, transform.position.y);
- } else if (transform.position.y <= downBound) {
- transform.position = new Vector2(transform.position.x, downBound);
- } else if (transform.position.y >= upBound) {
- transform.position = new Vector2(transform.position.x, upBound);
- } else {
- touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- }
- }
- if(Input.touchCount == 2){
- Touch touchZero = Input.GetTouch(0);
- Touch touchOne = Input.GetTouch(1);
- Vector2 touchZeroPrePos = touchZero.position - touchZero.deltaPosition;
- Vector2 touchOnePrePos = touchOne.position - touchOne.deltaPosition;
- float preMagnitude = (touchZeroPrePos - touchOnePrePos).magnitude;
- float currentMagnitude = (touchZero.position - touchOne.position).magnitude;
- float diff = currentMagnitude - preMagnitude;
- zoom(diff * 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 inc) {
- Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - inc, ZoomOutMin, ZoomOutMax);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment