SimpleViewController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Google.Maps.Examples.Shared {
  5. /// <summary>
  6. /// A simple controller, to allow user-controlled movement of a view aspect (typically a rig
  7. /// object to which a <see cref="Camera"/> is attached).
  8. /// Movement is performed via WASD keys (transverse), QE (up/down) and arrow keys (rotation).
  9. /// Speed of movement is higher when the controller is at higher altitude to make traversing a
  10. /// map more practical.
  11. /// </summary>
  12. public class SimpleViewController : MonoBehaviour {
  13. /// <summary>
  14. /// Max movement speed when pressing movement keys (WASD for panning, QE for up/down).
  15. /// </summary>
  16. [Tooltip("Max movement speed when pressing movement keys (WASD for panning, QE for up/down).")]
  17. public float MaxSpeed = 200f;
  18. /// <summary>
  19. /// Min movement speed when pressing movement keys (WASD for panning, QE for up/down).
  20. /// </summary>
  21. [Tooltip("Min movement speed when pressing movement keys (WASD for panning, QE for up/down).")]
  22. public float MinSpeed = 50f;
  23. /// <summary>
  24. /// Rotation speed when pressing arrow keys.
  25. /// </summary>
  26. [Tooltip("Rotation speed when pressing arrow keys.")]
  27. public float RotationSpeed = 100f;
  28. /// <summary>
  29. /// Minimum height off the ground.
  30. /// </summary>
  31. [Tooltip("Minimum height off the ground.")]
  32. public float MinAltitude = 2f;
  33. /// <summary>
  34. /// Maximum height off the ground.
  35. /// </summary>
  36. [Tooltip("Maximum height off the ground.")]
  37. public float MaxAltitude = 600f;
  38. /// <summary>
  39. /// Minimum angle relative to the ground.
  40. /// </summary>
  41. [Tooltip("Minimum angle above ground.")]
  42. public float MinInclination = 0;
  43. /// <summary>
  44. /// Maximum angle relative to the ground.
  45. /// </summary>
  46. [Tooltip("Maximum angle above ground.")]
  47. public float MaxInclination = 90;
  48. /// <summary>
  49. /// The current desired rotation of the Camera around the Y-Axis. Applied in world space after
  50. /// Inclination is applied.
  51. /// </summary>
  52. public float Azimuth;
  53. /// <summary>
  54. /// The current desired rotation of the Camera around the X-Axis. Applied in world space before
  55. /// Azimuth is applied.
  56. /// </summary>
  57. public float Inclination;
  58. private void Update() {
  59. Vector3 positionDelta = Vector3.zero;
  60. Vector3 rotationDelta = Vector3.zero;
  61. // ASWD keys used for position
  62. if (Input.GetKey(KeyCode.A)) {
  63. positionDelta.x -= 1.0f;
  64. }
  65. if (Input.GetKey(KeyCode.D)) {
  66. positionDelta.x += 1.0f;
  67. }
  68. if (Input.GetKey(KeyCode.W)) {
  69. positionDelta.z += 1.0f;
  70. }
  71. if (Input.GetKey(KeyCode.S)) {
  72. positionDelta.z -= 1.0f;
  73. }
  74. // QE keys used for altitude.
  75. if (Input.GetKey(KeyCode.Q)) {
  76. positionDelta.y -= 1.0f;
  77. }
  78. if (Input.GetKey(KeyCode.E)) {
  79. positionDelta.y += 1.0f;
  80. }
  81. // Arrow keys used to change camera orientation.
  82. if (Input.GetKey(KeyCode.LeftArrow)) {
  83. rotationDelta.x -= 1.0f;
  84. }
  85. if (Input.GetKey(KeyCode.RightArrow)) {
  86. rotationDelta.x += 1.0f;
  87. }
  88. if (Input.GetKey(KeyCode.UpArrow)) {
  89. rotationDelta.y -= 1.0f;
  90. }
  91. if (Input.GetKey(KeyCode.DownArrow)) {
  92. rotationDelta.y += 1.0f;
  93. }
  94. // Calculate the new Azimuth and Inclination.
  95. Azimuth += rotationDelta.x * RotationSpeed * Time.deltaTime;
  96. Azimuth = Azimuth % 360.0f;
  97. Inclination += rotationDelta.y * RotationSpeed * Time.deltaTime;
  98. Inclination = Mathf.Clamp(Inclination, MinInclination, MaxInclination);
  99. // Speed is affected linearly by the current altitude, and clamped to min/max range.
  100. float speed = Mathf.Clamp(transform.position.y, MinSpeed, MaxSpeed);
  101. // Calculate the current forward and right directions from the Azimuth and Inclination.
  102. Vector3 forward = Quaternion.Euler(0, Azimuth, 0) * Vector3.forward;
  103. Vector3 right = Quaternion.Euler(0, Azimuth, 0) * Vector3.right;
  104. // Combine 3 direction axes into a single motion vector.
  105. // Note: Up and down are always absolute, not affected by current orientation.
  106. Vector3 direction =
  107. (right * positionDelta.x + forward * positionDelta.z + positionDelta.y * Vector3.up);
  108. Vector3 motion = direction * speed * Time.deltaTime;
  109. Vector3 position = transform.position + motion;
  110. // Set current transform rotation using the Inclination and Azimuth.
  111. Quaternion rotation = Quaternion.Euler(Inclination, Azimuth, 0);
  112. // Enforce min/max height.
  113. position.y = Mathf.Clamp(position.y, MinAltitude, MaxAltitude);
  114. transform.position = position;
  115. transform.rotation = rotation;
  116. }
  117. }
  118. }