DragCamera.cs 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. namespace Shapes2D {
  2. using UnityEngine;
  3. using System.Collections;
  4. public class DragCamera : MonoBehaviour {
  5. Vector3 anchor;
  6. Vector3 origin;
  7. bool dragging;
  8. bool zoomed;
  9. float lastClick;
  10. void LateUpdate () {
  11. if (Input.GetMouseButtonDown(0)) {
  12. float time = Time.time;
  13. if (time - lastClick < 0.3f) {
  14. zoomed = !zoomed;
  15. if (zoomed)
  16. Camera.main.orthographicSize = 1;
  17. else
  18. Camera.main.orthographicSize = 7.35f;
  19. }
  20. lastClick = time;
  21. dragging = true;
  22. anchor = Input.mousePosition;
  23. origin = Camera.main.transform.position;
  24. }
  25. if (Input.GetMouseButtonUp(0)) {
  26. dragging = false;
  27. }
  28. if (dragging) {
  29. Vector3 delta = Camera.main.ScreenToWorldPoint(Input.mousePosition - anchor
  30. - new Vector3(-Screen.width / 2, -Screen.height / 2, Camera.main.transform.position.z))
  31. - new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, 0);
  32. Camera.main.transform.position = origin - delta;
  33. }
  34. }
  35. }
  36. }