CameraRotator.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. using UnityEngine.XR;
  16. namespace Vuplex.Demos {
  17. /// <summary>
  18. /// Script that makes it so that you can move the camera by holding down the control key on your
  19. /// keyboard and moving your mouse. When running on a device
  20. /// with a gyroscope, the gyroscope controls the camera rotation instead.
  21. /// </summary>
  22. class CameraRotator : MonoBehaviour {
  23. public GameObject InstructionMessage;
  24. private bool _legacyInputManagerDisabled;
  25. Vector2 _rotationFromMouse;
  26. // 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".
  27. #if !UNITY_WEBGL
  28. void Start() {
  29. // If XR is disabled, enable the gyro so that it can be used to control the camera rotation.
  30. if (!XRSettings.enabled) {
  31. Input.gyro.enabled = true;
  32. }
  33. #if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
  34. _legacyInputManagerDisabled = true;
  35. #endif
  36. // Show the instruction tip in the editor.
  37. if (Application.isEditor && InstructionMessage != null && !_legacyInputManagerDisabled) {
  38. InstructionMessage.SetActive(true);
  39. } else {
  40. InstructionMessage = null;
  41. }
  42. }
  43. /// <summary>
  44. /// If the device has a gyroscope, it is used to control the camera
  45. /// rotation. Otherwise, the user can hold down the control key on
  46. /// the keyboard to make the mouse control camera rotation.
  47. /// </summary>
  48. void Update() {
  49. // Dismiss the instruction message on the first click.
  50. if (InstructionMessage != null && !_legacyInputManagerDisabled && Input.GetMouseButtonDown(0)) {
  51. InstructionMessage.SetActive(false);
  52. InstructionMessage = null;
  53. }
  54. if (XRSettings.enabled) {
  55. // XR is enabled, so let the XR SDK control camera rotation instead.
  56. return;
  57. }
  58. if (SystemInfo.supportsGyroscope) {
  59. Camera.main.transform.Rotate(
  60. -Input.gyro.rotationRateUnbiased.x,
  61. -Input.gyro.rotationRateUnbiased.y,
  62. Input.gyro.rotationRateUnbiased.z
  63. );
  64. } else if (!_legacyInputManagerDisabled && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))) {
  65. float sensitivity = 10f;
  66. float maxYAngle = 80f;
  67. _rotationFromMouse.x += Input.GetAxis("Mouse X") * sensitivity;
  68. _rotationFromMouse.y -= Input.GetAxis("Mouse Y") * sensitivity;
  69. _rotationFromMouse.x = Mathf.Repeat(_rotationFromMouse.x, 360);
  70. _rotationFromMouse.y = Mathf.Clamp(_rotationFromMouse.y, -maxYAngle, maxYAngle);
  71. Camera.main.transform.rotation = Quaternion.Euler(_rotationFromMouse.y, _rotationFromMouse.x, 0);
  72. }
  73. }
  74. #endif
  75. }
  76. }