CameraRotator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. #if UNITY_2017_2_OR_NEWER
  16. using UnityEngine.XR;
  17. #else
  18. using XRSettings = UnityEngine.VR.VRSettings;
  19. #endif
  20. namespace Vuplex.Demos {
  21. /// <summary>
  22. /// Script that makes it so that you can move the camera by holding down the control key on your
  23. /// keyboard and moving your mouse. When running on a device
  24. /// with a gyroscope, the gyroscope controls the camera rotation instead.
  25. /// </summary>
  26. class CameraRotator : MonoBehaviour {
  27. public GameObject InstructionMessage;
  28. private bool _legacyInputManagerDisabled;
  29. Vector2 _rotationFromMouse;
  30. // Disable this functionality in the WebGL player because it causes the following error in Safari in Unity 2021.3 and newer: "ReferenceError: Can't find variable: DeviceOrientationEvent".
  31. #if !UNITY_WEBGL
  32. void Start() {
  33. // If XR is disabled, enable the gyro so that it can be used to control the camera rotation.
  34. if (!XRSettings.enabled) {
  35. Input.gyro.enabled = true;
  36. }
  37. #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
  38. _legacyInputManagerDisabled = true;
  39. #endif
  40. // Show the instruction tip in the editor.
  41. if (Application.isEditor && InstructionMessage != null && !_legacyInputManagerDisabled) {
  42. InstructionMessage.SetActive(true);
  43. } else {
  44. InstructionMessage = null;
  45. }
  46. }
  47. /// <summary>
  48. /// If the device has a gyroscope, it is used to control the camera
  49. /// rotation. Otherwise, the user can hold down the control key on
  50. /// the keyboard to make the mouse control camera rotation.
  51. /// </summary>
  52. void Update() {
  53. // Dismiss the instruction message on the first click.
  54. if (InstructionMessage != null && !_legacyInputManagerDisabled && Input.GetMouseButtonDown(0)) {
  55. InstructionMessage.SetActive(false);
  56. InstructionMessage = null;
  57. }
  58. if (XRSettings.enabled) {
  59. // XR is enabled, so let the XR SDK control camera rotation instead.
  60. return;
  61. }
  62. if (SystemInfo.supportsGyroscope) {
  63. Camera.main.transform.Rotate(
  64. -Input.gyro.rotationRateUnbiased.x,
  65. -Input.gyro.rotationRateUnbiased.y,
  66. Input.gyro.rotationRateUnbiased.z
  67. );
  68. } else if (!_legacyInputManagerDisabled && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) {
  69. float sensitivity = 10f;
  70. float maxYAngle = 80f;
  71. _rotationFromMouse.x += Input.GetAxis("Mouse X") * sensitivity;
  72. _rotationFromMouse.y -= Input.GetAxis("Mouse Y") * sensitivity;
  73. _rotationFromMouse.x = Mathf.Repeat(_rotationFromMouse.x, 360);
  74. _rotationFromMouse.y = Mathf.Clamp(_rotationFromMouse.y, -maxYAngle, maxYAngle);
  75. Camera.main.transform.rotation = Quaternion.Euler(_rotationFromMouse.y, _rotationFromMouse.x, 0);
  76. }
  77. }
  78. #endif
  79. }
  80. }