Advertisement
Sabotender

Simple Map Movement (Pan, Rotate, Zoom)

Feb 18th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Attach to an Empty GameObject, With Your camera as a child
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. public class MapCamera : MonoBehaviour {
  7.     public float panSpeed = 0.5f;
  8.     public KeyCode panKey = KeyCode.None;
  9.     public bool invertPanX = true;
  10.     public bool invertPanY = true;
  11.  
  12.     public float rotateSpeed = 2;
  13.     public KeyCode rotateKey = KeyCode.LeftControl;
  14.     public bool invertRotateX = true;
  15.     public bool invertRotateY = false;
  16.  
  17.     public float zoomSpeed = 1;
  18.     public bool useMouseWheel = true;
  19.     public KeyCode zoomKey = KeyCode.LeftShift;
  20.     public bool invertZoom = false;
  21.  
  22.     private bool _isDragging;
  23.     private Vector2 lastMousePos;
  24.     private Transform t;
  25.     private Camera c;
  26.  
  27.     public bool IsCameraBeingDragged {
  28.         get { return _isDragging; }
  29.     }
  30.  
  31.     void Start () {
  32.         t = transform;
  33.         c = GetComponentInChildren<Camera>();
  34.     }
  35.     // Update is called once per frame
  36.     void Update () {
  37.         //When clicking the mouse down
  38.         if(Input.GetMouseButtonDown(0)) {
  39.             _isDragging = true;
  40.             lastMousePos = Input.mousePosition;
  41.         }
  42.  
  43.         //While dragging
  44.         if(Input.GetMouseButton(0)) {
  45.             Vector2 mousePos = Input.mousePosition;
  46.             if(rotateKey != KeyCode.None && Input.GetKey(rotateKey)) {
  47.                 t.Rotate(new Vector3((mousePos.y - lastMousePos.y) * (invertRotateY?-1:1), (mousePos.x - lastMousePos.x) * (invertRotateX?-1:1), 0) * Time.deltaTime * rotateSpeed, Space.Self);
  48.                 t.localRotation = Quaternion.Euler(new Vector3(t.localRotation.eulerAngles.x, t.localRotation.eulerAngles.y, 0));
  49.             } else if(zoomKey != KeyCode.None && Input.GetKey(zoomKey)) {
  50.                 c.transform.Translate(c.transform.forward * (mousePos.y - lastMousePos.y) * (invertZoom?-1:1) * Time.deltaTime * zoomSpeed);
  51.             } else if(panKey == KeyCode.None || Input.GetKey(panKey)) {
  52.                 t.Translate(new Vector3((mousePos.x - lastMousePos.x) * (invertPanX?-1:1), 0, (mousePos.y - lastMousePos.y) * (invertPanY?-1:1)) * Time.deltaTime * panSpeed);
  53.             }
  54.             lastMousePos = Input.mousePosition;
  55.             //c.transform.LookAt(t);
  56.         }
  57.  
  58.         //When releasing the mouse
  59.         if(Input.GetMouseButtonUp(0)){
  60.             _isDragging = false;
  61.         }
  62.  
  63.         if(Input.GetAxis("Mouse ScrollWheel") != 0) {
  64.             c.transform.Translate(c.transform.forward * Input.GetAxis("Mouse ScrollWheel") * (invertZoom?-100:100) * Time.deltaTime * zoomSpeed);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement