Guest User

Untitled

a guest
Jan 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. public class ControlCamera : MonoBehaviour
  2. {
  3.  
  4. private Vector2?[] oldTouchPositions = {
  5. null,
  6. null
  7. };
  8.  
  9. private Vector2 oldTouchVector;
  10.  
  11. private float oldTouchDistance;
  12.  
  13. private Vector3 bottomLeft;
  14. private Vector3 topRight;
  15.  
  16. private float cameraMaxY;
  17. private float cameraMinY;
  18. private float cameraMaxX;
  19. private float cameraMinX;
  20.  
  21. public float maxZoom;
  22. public float minZoom;
  23.  
  24. private void Start()
  25. {
  26. // Set max camera bounds
  27. topRight = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(GetComponent<Camera>().pixelWidth, GetComponent<Camera>().pixelHeight, -transform.position.z));
  28. bottomLeft = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, 0, -transform.position.z));
  29. cameraMaxX = topRight.x;
  30. cameraMaxY = topRight.y;
  31. cameraMinX = bottomLeft.x;
  32. cameraMinY = bottomLeft.y;
  33. }
  34.  
  35. private void Update()
  36. {
  37. // No touches
  38. if (Input.touchCount == 0)
  39. {
  40. oldTouchPositions[0] = null;
  41. oldTouchPositions[1] = null;
  42. }
  43. // 1 Touch
  44. else if (Input.touchCount == 1)
  45. {
  46. if (oldTouchPositions[0] == null || oldTouchPositions[1] != null)
  47. {
  48. oldTouchPositions[0] = Input.GetTouch(0).position;
  49. oldTouchPositions[1] = null;
  50. }
  51. else
  52. {
  53. Vector2 newTouchPosition = Input.GetTouch(0).position;
  54.  
  55. transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * GetComponent<Camera>().orthographicSize / GetComponent<Camera>().pixelHeight * 2f));
  56.  
  57. oldTouchPositions[0] = newTouchPosition;
  58. }
  59. // 2 touches or more
  60. } else {
  61.  
  62. if (oldTouchPositions[1] == null)
  63. {
  64. oldTouchPositions[0] = Input.GetTouch(0).position;
  65. oldTouchPositions[1] = Input.GetTouch(1).position;
  66. oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
  67. oldTouchDistance = oldTouchVector.magnitude;
  68.  
  69. } else {
  70.  
  71. Vector2 screen = new Vector2(GetComponent<Camera>().pixelWidth, GetComponent<Camera>().pixelHeight);
  72.  
  73. Vector2[] newTouchPositions = {
  74. Input.GetTouch(0).position,
  75. Input.GetTouch(1).position
  76. };
  77. Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
  78. float newTouchDistance = newTouchVector.magnitude;
  79.  
  80. transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * GetComponent<Camera>().orthographicSize / screen.y));
  81. GetComponent<Camera>().orthographicSize *= oldTouchDistance / newTouchDistance;
  82. transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * GetComponent<Camera>().orthographicSize / screen.y);
  83.  
  84. oldTouchPositions[0] = newTouchPositions[0];
  85. oldTouchPositions[1] = newTouchPositions[1];
  86. oldTouchVector = newTouchVector;
  87. oldTouchDistance = newTouchDistance;
  88.  
  89. // --------------------------------------------------------------->>
  90.  
  91. // Control Zoom In
  92. if (GetComponent<Camera>().orthographicSize > minZoom)
  93. {
  94. //
  95. }
  96.  
  97. // Control Zoom Out
  98. if (GetComponent<Camera>().orthographicSize < maxZoom)
  99. {
  100. //
  101. }
  102.  
  103. // --------------------------------------------------------------->>
  104. }
  105. }
  106.  
  107. // Check if camera is out-of-bounds, if so, move back in-bounds
  108. topRight = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(GetComponent<Camera>().pixelWidth, GetComponent<Camera>().pixelHeight, -transform.position.z));
  109. bottomLeft = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(0, 0, -transform.position.z));
  110.  
  111. if (topRight.x > cameraMaxX)
  112. {
  113. transform.position = new Vector3(transform.position.x - (topRight.x - cameraMaxX), transform.position.y, transform.position.z);
  114. }
  115.  
  116. if (topRight.y > cameraMaxY)
  117. {
  118. transform.position = new Vector3(transform.position.x, transform.position.y - (topRight.y - cameraMaxY), transform.position.z);
  119. }
  120.  
  121. if (bottomLeft.x < cameraMinX)
  122. {
  123. transform.position = new Vector3(transform.position.x + (cameraMinX - bottomLeft.x), transform.position.y, transform.position.z);
  124. }
  125.  
  126. if (bottomLeft.y < cameraMinY)
  127. {
  128. transform.position = new Vector3(transform.position.x, transform.position.y + (cameraMinY - bottomLeft.y), transform.position.z);
  129. }
  130. }
  131. }
Add Comment
Please, Sign In to add comment