OrbitControls.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. namespace Animancer.Examples
  4. {
  5. /// <summary>Simple mouse controls for orbiting the camera around a focal point.</summary>
  6. /// <remarks>
  7. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/examples/basics/scene-setup#orbit-controls">Orbit Controls</see>
  8. /// </remarks>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Examples/OrbitControls
  10. ///
  11. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Orbit Controls")]
  12. [HelpURL(Strings.DocsURLs.APIDocumentation + "." + nameof(Examples) + "/" + nameof(OrbitControls))]
  13. [ExecuteAlways]
  14. public sealed class OrbitControls : MonoBehaviour
  15. {
  16. /************************************************************************************************************************/
  17. [SerializeField] private Vector3 _FocalPoint = new Vector3(0, 1, 0);
  18. [SerializeField] private Vector3 _Sensitivity = new Vector3(1, -0.75f, -0.1f);
  19. [SerializeField] private float _MinZoom = 0.5f;
  20. private float _Distance;
  21. /************************************************************************************************************************/
  22. private void Awake()
  23. {
  24. _Distance = Vector3.Distance(_FocalPoint, transform.position);
  25. transform.LookAt(_FocalPoint);
  26. }
  27. /************************************************************************************************************************/
  28. private void Update()
  29. {
  30. #if UNITY_EDITOR
  31. if (!UnityEditor.EditorApplication.isPlaying)
  32. {
  33. transform.LookAt(_FocalPoint);
  34. return;
  35. }
  36. #endif
  37. if (ExampleInput.RightMouseHold)
  38. {
  39. var movement = ExampleInput.MousePositionDelta;
  40. if (!movement.Equals(default))
  41. {
  42. var euler = transform.localEulerAngles;
  43. euler.y += movement.x * _Sensitivity.x;
  44. euler.x += movement.y * _Sensitivity.y;
  45. if (euler.x > 180)
  46. euler.x -= 360;
  47. euler.x = Mathf.Clamp(euler.x, -80, 80);
  48. transform.localEulerAngles = euler;
  49. }
  50. }
  51. // Scroll to zoom if the mouse is currently inside the game window.
  52. var zoom = ExampleInput.MouseScrollDelta.y * _Sensitivity.z;
  53. var mousePosition = ExampleInput.MousePosition;
  54. if (zoom != 0 &&
  55. mousePosition.x >= 0 && mousePosition.x <= Screen.width &&
  56. mousePosition.y >= 0 && mousePosition.y <= Screen.height)
  57. {
  58. _Distance *= 1 + zoom;
  59. if (_Distance < _MinZoom)
  60. _Distance = _MinZoom;
  61. }
  62. // Always update position even with no input in case the target is moving.
  63. UpdatePosition();
  64. }
  65. /************************************************************************************************************************/
  66. private void UpdatePosition()
  67. {
  68. transform.position = _FocalPoint - transform.forward * _Distance;
  69. }
  70. /************************************************************************************************************************/
  71. private void OnDrawGizmosSelected()
  72. {
  73. Gizmos.color = new Color(0.5f, 1, 0.5f, 1);
  74. Gizmos.DrawLine(transform.position, _FocalPoint);
  75. }
  76. /************************************************************************************************************************/
  77. }
  78. }