CameraRotator.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2022 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.WebView.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. void Start() {
  29. // If VR 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. // Show the instruction tip in the editor.
  34. if (Application.isEditor && InstructionMessage != null) {
  35. InstructionMessage.SetActive(true);
  36. } else {
  37. InstructionMessage = null;
  38. }
  39. }
  40. /// <summary>
  41. /// If the device has a gyroscope, it is used to control the camera
  42. /// rotation. Otherwise, the user can hold down the control key on
  43. /// the keyboard to make the mouse control camera rotation.
  44. /// </summary>
  45. void Update() {
  46. // Dismiss the instruction message on the first click.
  47. if (InstructionMessage != null && Input.GetMouseButtonDown(0)) {
  48. InstructionMessage.SetActive(false);
  49. InstructionMessage = null;
  50. }
  51. if (XRSettings.enabled) {
  52. // VR is enabled, so let the VR SDK control camera rotation instead.
  53. return;
  54. }
  55. if (SystemInfo.supportsGyroscope) {
  56. Camera.main.transform.Rotate(
  57. -Input.gyro.rotationRateUnbiased.x,
  58. -Input.gyro.rotationRateUnbiased.y,
  59. Input.gyro.rotationRateUnbiased.z
  60. );
  61. } else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) {
  62. float sensitivity = 10f;
  63. float maxYAngle = 80f;
  64. _rotationFromMouse.x += Input.GetAxis("Mouse X") * sensitivity;
  65. _rotationFromMouse.y -= Input.GetAxis("Mouse Y") * sensitivity;
  66. _rotationFromMouse.x = Mathf.Repeat(_rotationFromMouse.x, 360);
  67. _rotationFromMouse.y = Mathf.Clamp(_rotationFromMouse.y, -maxYAngle, maxYAngle);
  68. Camera.main.transform.rotation = Quaternion.Euler(_rotationFromMouse.y, _rotationFromMouse.x, 0);
  69. }
  70. }
  71. Vector2 _rotationFromMouse;
  72. }
  73. }