Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. // Mouse navigation for Unity
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class WorldNavigation : MonoBehaviour {
  8.  
  9. public Camera cam;
  10. private bool isPanning;
  11. private bool isRotating;
  12. private bool isTilting;
  13. private bool isZooming;
  14.  
  15. private Vector3 touchStart;
  16. public float groundZ = 0;
  17.  
  18. //rotation params
  19. public float speedH = 2.0f;
  20. public float speedV = 2.0f;
  21.  
  22. private float yaw;
  23. private float pitch;
  24.  
  25. //tilting variables
  26. private Vector3 pivot;
  27. private Vector3 rotationAxis;
  28. private float y_rotate;
  29. private float x_rotate;
  30. public float rotationSpeed = 10f;
  31.  
  32. // Update is called once per frame
  33. void Update () {
  34.  
  35. // --GETTING INPUT--
  36.  
  37. // Left mouse button: Panning
  38. if (Input.GetMouseButtonDown (0)) {
  39. touchStart = mouseWorldPosition (groundZ);
  40. isPanning = true;
  41. };
  42. if (Input.GetMouseButton (0)) {
  43. Vector3 direction = touchStart - mouseWorldPosition (groundZ);
  44. cam.transform.position += direction;
  45. }
  46.  
  47. // Right mouse button: Rotating
  48. if (Input.GetMouseButtonDown (1)) {
  49. Vector3 angles = cam.transform.eulerAngles;
  50. yaw = angles.y;
  51. pitch = angles.x;
  52. isRotating = true;
  53. }
  54.  
  55. // Middle mouse button: Tilting
  56. float currentCamX = cam.transform.position.x;
  57. float currentCamZ = cam.transform.position.z;
  58. if (cam.transform.position.y < 0.5f) {
  59. cam.transform.position = new Vector3 (currentCamX, 0.5f, currentCamZ);
  60. }
  61. if (Input.GetMouseButtonDown (2)) {
  62. pivot = centerScreenPosition (groundZ);
  63. isTilting = true;
  64. }
  65.  
  66. // Middle Mouse Scroll
  67. if (Input.GetAxis ("Mouse ScrollWheel") != 0) {
  68. isZooming = true;
  69. }
  70.  
  71. // canceling all functions
  72. if (!Input.GetMouseButton (0)) isPanning = false;
  73. if (!Input.GetMouseButton (1)) isRotating = false;
  74. if (!Input.GetMouseButton (2)) isTilting = false;
  75. if (Input.GetAxis ("Mouse ScrollWheel") == 0) isZooming = false;
  76. }
  77.  
  78. // LATE UPDATE
  79. void LateUpdate () {
  80.  
  81. //Right Button: Camera Rotation
  82. if (isRotating) {
  83. yaw += speedH * Input.GetAxis ("Mouse X");
  84. pitch -= speedV * Input.GetAxis ("Mouse Y");
  85. // Clamp pitch:
  86. pitch = Mathf.Clamp (pitch, -90f, 90f);
  87. Quaternion rotation = Quaternion.Euler (pitch, yaw, 0);
  88. cam.transform.rotation = rotation;
  89. }
  90.  
  91. //middle mouse: tilting
  92. if (isTilting) {
  93. y_rotate = Input.GetAxis ("Mouse X") * rotationSpeed;
  94. x_rotate = Input.GetAxis ("Mouse Y") * rotationSpeed;
  95. //cam.transform.RotateAround (pivot, Vector3.left, rotationAngleOnX);
  96. OrbitCamera (cam, pivot, y_rotate, x_rotate);
  97. }
  98.  
  99. // mouse scrollwheel: zooming
  100. if (isZooming) {
  101. Vector3 desiredPosition;
  102. desiredPosition = mouseWorldPosition (groundZ);
  103. float distance = Vector3.Distance (desiredPosition, transform.position);
  104. Vector3 direction = Vector3.Normalize (desiredPosition - transform.position) * (distance * Input.GetAxis ("Mouse ScrollWheel"));
  105. transform.position += direction;
  106. }
  107.  
  108. }
  109.  
  110. // Raycasting Mouse Position for Panning
  111. private Vector3 mouseWorldPosition (float z) {
  112. Ray mousePos = cam.ScreenPointToRay (Input.mousePosition);
  113. Plane ground = new Plane (Vector3.forward, new Vector3 (0, 0, z));
  114. float distance;
  115. ground.Raycast (mousePos, out distance);
  116. return mousePos.GetPoint (distance);
  117. }
  118.  
  119. // Raycasting center of screen for Tilting
  120. private Vector3 centerScreenPosition (float z) {
  121. //Ray mousePos = cam.ScreenPointToRay(Input.mousePosition);
  122. Ray centerScreen = cam.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0f));
  123. Plane ground = new Plane (Vector3.forward, new Vector3 (0, 0, z));
  124. float distance;
  125. //ground.Raycast(mousePos, out distance);
  126. ground.Raycast (centerScreen, out distance);
  127. //return mousePos.GetPoint(distance);
  128. return centerScreen.GetPoint (distance);
  129. }
  130.  
  131. // calculating orbit camera
  132. public void OrbitCamera (Camera cam, Vector3 target, float y_rotate, float x_rotate) {
  133. Vector3 angles = transform.eulerAngles;
  134. angles.z = 0;
  135. cam.transform.eulerAngles = angles;
  136. cam.transform.RotateAround (target, Vector3.up, y_rotate);
  137. cam.transform.RotateAround (target, Vector3.left, x_rotate);
  138. cam.transform.LookAt (target);
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement