FreeCamera.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Utility Free Camera component.
  7. /// </summary>
  8. [HelpURL(Documentation.baseURL + Documentation.version + Documentation.subURL + "Free-Camera" + Documentation.endURL)]
  9. [ExecuteAlways]
  10. public class FreeCamera : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// Rotation speed when using a controller.
  14. /// </summary>
  15. public float m_LookSpeedController = 120f;
  16. /// <summary>
  17. /// Rotation speed when using the mouse.
  18. /// </summary>
  19. public float m_LookSpeedMouse = 10.0f;
  20. /// <summary>
  21. /// Movement speed.
  22. /// </summary>
  23. public float m_MoveSpeed = 10.0f;
  24. /// <summary>
  25. /// Value added to the speed when incrementing.
  26. /// </summary>
  27. public float m_MoveSpeedIncrement = 2.5f;
  28. /// <summary>
  29. /// Scale factor of the turbo mode.
  30. /// </summary>
  31. public float m_Turbo = 10.0f;
  32. private static string kMouseX = "Mouse X";
  33. private static string kMouseY = "Mouse Y";
  34. private static string kRightStickX = "Controller Right Stick X";
  35. private static string kRightStickY = "Controller Right Stick Y";
  36. private static string kVertical = "Vertical";
  37. private static string kHorizontal = "Horizontal";
  38. private static string kYAxis = "YAxis";
  39. private static string kSpeedAxis = "Speed Axis";
  40. void OnEnable()
  41. {
  42. RegisterInputs();
  43. }
  44. void RegisterInputs()
  45. {
  46. #if UNITY_EDITOR
  47. List<InputManagerEntry> inputEntries = new List<InputManagerEntry>();
  48. // Add new bindings
  49. inputEntries.Add(new InputManagerEntry { name = kRightStickX, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fourth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f });
  50. inputEntries.Add(new InputManagerEntry { name = kRightStickY, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fifth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f, invert = true });
  51. inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 5", btnNegative = "page down", altBtnNegative = "joystick button 4", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  52. inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "home", btnNegative = "end", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  53. inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  54. InputRegistering.RegisterInputs(inputEntries);
  55. #endif
  56. }
  57. void Update()
  58. {
  59. // If the debug menu is running, we don't want to conflict with its inputs.
  60. if (DebugManager.instance.displayRuntimeUI)
  61. return;
  62. float inputRotateAxisX = 0.0f;
  63. float inputRotateAxisY = 0.0f;
  64. if (Input.GetMouseButton(1))
  65. {
  66. inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
  67. inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
  68. }
  69. inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * Time.deltaTime);
  70. inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * Time.deltaTime);
  71. float inputChangeSpeed = Input.GetAxis(kSpeedAxis);
  72. if (inputChangeSpeed != 0.0f)
  73. {
  74. m_MoveSpeed += inputChangeSpeed * m_MoveSpeedIncrement;
  75. if (m_MoveSpeed < m_MoveSpeedIncrement) m_MoveSpeed = m_MoveSpeedIncrement;
  76. }
  77. float inputVertical = Input.GetAxis(kVertical);
  78. float inputHorizontal = Input.GetAxis(kHorizontal);
  79. float inputYAxis = Input.GetAxis(kYAxis);
  80. bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f || inputYAxis != 0.0f;
  81. if (moved)
  82. {
  83. float rotationX = transform.localEulerAngles.x;
  84. float newRotationY = transform.localEulerAngles.y + inputRotateAxisX;
  85. // Weird clamping code due to weird Euler angle mapping...
  86. float newRotationX = (rotationX - inputRotateAxisY);
  87. if (rotationX <= 90.0f && newRotationX >= 0.0f)
  88. newRotationX = Mathf.Clamp(newRotationX, 0.0f, 90.0f);
  89. if (rotationX >= 270.0f)
  90. newRotationX = Mathf.Clamp(newRotationX, 270.0f, 360.0f);
  91. transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);
  92. float moveSpeed = Time.deltaTime * m_MoveSpeed;
  93. if (Input.GetMouseButton(1))
  94. moveSpeed *= Input.GetKey(KeyCode.LeftShift) ? m_Turbo : 1.0f;
  95. else
  96. moveSpeed *= Input.GetAxis("Fire1") > 0.0f ? m_Turbo : 1.0f;
  97. transform.position += transform.forward * moveSpeed * inputVertical;
  98. transform.position += transform.right * moveSpeed * inputHorizontal;
  99. transform.position += Vector3.up * moveSpeed * inputYAxis;
  100. }
  101. }
  102. }
  103. }