Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraCtrl : MonoBehaviour {
  6.  
  7. public Camera[] SyncedCameras;
  8. public float Radius = 5;
  9. public float theta = 0;
  10. public float phi = 0;
  11.  
  12. private const float angleSpeed = 0.005f;
  13. private const float radiusSpeed = 0.1f;
  14. private const float radiusFar = 100;
  15. private const float radiusNear = 0.1f;
  16. private const float phiLimit = Mathf.PI * 0.45f;
  17.  
  18. // Use this for initialization
  19. void Start () {
  20. mouseLeftPressed = false;
  21. mouseRightPressed = false;
  22. UpdateCameras();
  23. }
  24.  
  25. // Update is called once per frame
  26. void Update ()
  27. {
  28. bool cameraDirty = false;
  29. if ((Input.GetMouseButton(0) == true) || (Input.GetMouseButton(1) == true))
  30. {
  31. //Mouse left - rotation
  32. if (Input.GetMouseButton(0) == true)
  33. {
  34. if (mouseLeftPressed == false)
  35. {
  36. mouseLeftPressed = true;
  37. }
  38. else
  39. {
  40. float mouseDeltaX = Input.mousePosition.x - mouseLastX;
  41. float mouseDeltaY = Input.mousePosition.y - mouseLastY;
  42. theta += mouseDeltaX * angleSpeed;
  43. if (theta > Mathf.PI) theta -= 2 * Mathf.PI;
  44. if (theta < -Mathf.PI) theta += 2 * Mathf.PI;
  45. phi += - mouseDeltaY * angleSpeed;
  46. if (phi > phiLimit) phi = phiLimit;
  47. if (phi < -phiLimit) phi = -phiLimit;
  48. cameraDirty = true;
  49. }
  50. } else
  51. {
  52. mouseLeftPressed = false;
  53. }
  54.  
  55. //Mouse right button -Zooming (radius)
  56. if (Input.GetMouseButton(1) == true)
  57. {
  58. if (mouseRightPressed == false)
  59. {
  60. mouseRightPressed = true;
  61. }
  62. else
  63. {
  64. float mouseDeltaY = Input.mousePosition.y - mouseLastY;
  65. Radius += - mouseDeltaY * radiusSpeed;
  66. if (Radius < radiusNear) Radius = radiusNear;
  67. if (Radius > radiusFar) Radius = - radiusFar;
  68. cameraDirty = true;
  69. }
  70. }
  71. }
  72.  
  73. if (cameraDirty) UpdateCameras();
  74. mouseLastX = Input.mousePosition.x;
  75. mouseLastY = Input.mousePosition.y;
  76. }
  77.  
  78. void UpdateCameras()
  79. {
  80. Vector3 CameraPos = new Vector3(Mathf.Sin(theta),
  81. Mathf.Tan(phi),
  82. Mathf.Cos(theta));
  83. CameraPos.Normalize();
  84. CameraPos *= Radius;
  85.  
  86. foreach (Camera cam in SyncedCameras)
  87. {
  88. cam.transform.position = CameraPos;
  89. cam.transform.LookAt(Vector3.zero);
  90. }
  91. }
  92.  
  93. bool mouseLeftPressed, mouseRightPressed;
  94. float mouseLastX, mouseLastY;
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement