Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Attach to an Empty GameObject, With Your camera as a child
- using UnityEngine;
- using System.Collections;
- public class MapCamera : MonoBehaviour {
- public float panSpeed = 0.5f;
- public KeyCode panKey = KeyCode.None;
- public bool invertPanX = true;
- public bool invertPanY = true;
- public float rotateSpeed = 2;
- public KeyCode rotateKey = KeyCode.LeftControl;
- public bool invertRotateX = true;
- public bool invertRotateY = false;
- public float zoomSpeed = 1;
- public bool useMouseWheel = true;
- public KeyCode zoomKey = KeyCode.LeftShift;
- public bool invertZoom = false;
- private bool _isDragging;
- private Vector2 lastMousePos;
- private Transform t;
- private Camera c;
- public bool IsCameraBeingDragged {
- get { return _isDragging; }
- }
- void Start () {
- t = transform;
- c = GetComponentInChildren<Camera>();
- }
- // Update is called once per frame
- void Update () {
- //When clicking the mouse down
- if(Input.GetMouseButtonDown(0)) {
- _isDragging = true;
- lastMousePos = Input.mousePosition;
- }
- //While dragging
- if(Input.GetMouseButton(0)) {
- Vector2 mousePos = Input.mousePosition;
- if(rotateKey != KeyCode.None && Input.GetKey(rotateKey)) {
- t.Rotate(new Vector3((mousePos.y - lastMousePos.y) * (invertRotateY?-1:1), (mousePos.x - lastMousePos.x) * (invertRotateX?-1:1), 0) * Time.deltaTime * rotateSpeed, Space.Self);
- t.localRotation = Quaternion.Euler(new Vector3(t.localRotation.eulerAngles.x, t.localRotation.eulerAngles.y, 0));
- } else if(zoomKey != KeyCode.None && Input.GetKey(zoomKey)) {
- c.transform.Translate(c.transform.forward * (mousePos.y - lastMousePos.y) * (invertZoom?-1:1) * Time.deltaTime * zoomSpeed);
- } else if(panKey == KeyCode.None || Input.GetKey(panKey)) {
- t.Translate(new Vector3((mousePos.x - lastMousePos.x) * (invertPanX?-1:1), 0, (mousePos.y - lastMousePos.y) * (invertPanY?-1:1)) * Time.deltaTime * panSpeed);
- }
- lastMousePos = Input.mousePosition;
- //c.transform.LookAt(t);
- }
- //When releasing the mouse
- if(Input.GetMouseButtonUp(0)){
- _isDragging = false;
- }
- if(Input.GetAxis("Mouse ScrollWheel") != 0) {
- c.transform.Translate(c.transform.forward * Input.GetAxis("Mouse ScrollWheel") * (invertZoom?-100:100) * Time.deltaTime * zoomSpeed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement