No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FreeCamera.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
  2. #define USE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. #endif
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// Utility Free Camera component.
  11. /// </summary>
  12. [CoreRPHelpURLAttribute("Free-Camera")]
  13. public class FreeCamera : MonoBehaviour
  14. {
  15. const float k_MouseSensitivityMultiplier = 0.01f;
  16. /// <summary>
  17. /// Rotation speed when using a controller.
  18. /// </summary>
  19. public float m_LookSpeedController = 120f;
  20. /// <summary>
  21. /// Rotation speed when using the mouse.
  22. /// </summary>
  23. public float m_LookSpeedMouse = 4.0f;
  24. /// <summary>
  25. /// Movement speed.
  26. /// </summary>
  27. public float m_MoveSpeed = 10.0f;
  28. /// <summary>
  29. /// Value added to the speed when incrementing.
  30. /// </summary>
  31. public float m_MoveSpeedIncrement = 2.5f;
  32. /// <summary>
  33. /// Scale factor of the turbo mode.
  34. /// </summary>
  35. public float m_Turbo = 10.0f;
  36. #if !USE_INPUT_SYSTEM
  37. private static string kMouseX = "Mouse X";
  38. private static string kMouseY = "Mouse Y";
  39. private static string kRightStickX = "Controller Right Stick X";
  40. private static string kRightStickY = "Controller Right Stick Y";
  41. private static string kVertical = "Vertical";
  42. private static string kHorizontal = "Horizontal";
  43. private static string kYAxis = "YAxis";
  44. private static string kSpeedAxis = "Speed Axis";
  45. #endif
  46. #if USE_INPUT_SYSTEM
  47. InputAction lookAction;
  48. InputAction moveAction;
  49. InputAction speedAction;
  50. InputAction yMoveAction;
  51. #endif
  52. void OnEnable()
  53. {
  54. RegisterInputs();
  55. }
  56. void RegisterInputs()
  57. {
  58. #if USE_INPUT_SYSTEM
  59. var map = new InputActionMap("Free Camera");
  60. lookAction = map.AddAction("look", binding: "<Mouse>/delta");
  61. moveAction = map.AddAction("move", binding: "<Gamepad>/leftStick");
  62. speedAction = map.AddAction("speed", binding: "<Gamepad>/dpad");
  63. yMoveAction = map.AddAction("yMove");
  64. lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
  65. moveAction.AddCompositeBinding("Dpad")
  66. .With("Up", "<Keyboard>/w")
  67. .With("Up", "<Keyboard>/upArrow")
  68. .With("Down", "<Keyboard>/s")
  69. .With("Down", "<Keyboard>/downArrow")
  70. .With("Left", "<Keyboard>/a")
  71. .With("Left", "<Keyboard>/leftArrow")
  72. .With("Right", "<Keyboard>/d")
  73. .With("Right", "<Keyboard>/rightArrow");
  74. speedAction.AddCompositeBinding("Dpad")
  75. .With("Up", "<Keyboard>/home")
  76. .With("Down", "<Keyboard>/end");
  77. yMoveAction.AddCompositeBinding("Dpad")
  78. .With("Up", "<Keyboard>/pageUp")
  79. .With("Down", "<Keyboard>/pageDown")
  80. .With("Up", "<Keyboard>/e")
  81. .With("Down", "<Keyboard>/q")
  82. .With("Up", "<Gamepad>/rightshoulder")
  83. .With("Down", "<Gamepad>/leftshoulder");
  84. moveAction.Enable();
  85. lookAction.Enable();
  86. speedAction.Enable();
  87. yMoveAction.Enable();
  88. #endif
  89. #if UNITY_EDITOR && !USE_INPUT_SYSTEM
  90. List<InputManagerEntry> inputEntries = new List<InputManagerEntry>();
  91. // Add new bindings
  92. inputEntries.Add(new InputManagerEntry { name = kRightStickX, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fourth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f });
  93. 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 });
  94. 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 });
  95. inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "q", btnNegative = "e", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  96. inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "home", btnNegative = "end", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  97. inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
  98. InputRegistering.RegisterInputs(inputEntries);
  99. #endif
  100. }
  101. float inputRotateAxisX, inputRotateAxisY;
  102. float inputChangeSpeed;
  103. float inputVertical, inputHorizontal, inputYAxis;
  104. bool leftShiftBoost, leftShift, fire1;
  105. void UpdateInputs()
  106. {
  107. inputRotateAxisX = 0.0f;
  108. inputRotateAxisY = 0.0f;
  109. leftShiftBoost = false;
  110. fire1 = false;
  111. #if USE_INPUT_SYSTEM
  112. var lookDelta = lookAction.ReadValue<Vector2>();
  113. inputRotateAxisX = lookDelta.x * m_LookSpeedMouse * k_MouseSensitivityMultiplier;
  114. inputRotateAxisY = lookDelta.y * m_LookSpeedMouse * k_MouseSensitivityMultiplier;
  115. leftShift = Keyboard.current?.leftShiftKey?.isPressed ?? false;
  116. fire1 = Mouse.current?.leftButton?.isPressed == true || Gamepad.current?.xButton?.isPressed == true;
  117. inputChangeSpeed = speedAction.ReadValue<Vector2>().y;
  118. var moveDelta = moveAction.ReadValue<Vector2>();
  119. inputVertical = moveDelta.y;
  120. inputHorizontal = moveDelta.x;
  121. inputYAxis = yMoveAction.ReadValue<Vector2>().y;
  122. #else
  123. if (Input.GetMouseButton(1))
  124. {
  125. leftShiftBoost = true;
  126. inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
  127. inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
  128. }
  129. inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * k_MouseSensitivityMultiplier);
  130. inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * k_MouseSensitivityMultiplier);
  131. leftShift = Input.GetKey(KeyCode.LeftShift);
  132. fire1 = Input.GetAxis("Fire1") > 0.0f;
  133. inputChangeSpeed = Input.GetAxis(kSpeedAxis);
  134. inputVertical = Input.GetAxis(kVertical);
  135. inputHorizontal = Input.GetAxis(kHorizontal);
  136. inputYAxis = Input.GetAxis(kYAxis);
  137. #endif
  138. }
  139. void Update()
  140. {
  141. // If the debug menu is running, we don't want to conflict with its inputs.
  142. if (DebugManager.instance.displayRuntimeUI)
  143. return;
  144. UpdateInputs();
  145. if (inputChangeSpeed != 0.0f)
  146. {
  147. m_MoveSpeed += inputChangeSpeed * m_MoveSpeedIncrement;
  148. if (m_MoveSpeed < m_MoveSpeedIncrement) m_MoveSpeed = m_MoveSpeedIncrement;
  149. }
  150. bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f || inputYAxis != 0.0f;
  151. if (moved)
  152. {
  153. float rotationX = transform.localEulerAngles.x;
  154. float newRotationY = transform.localEulerAngles.y + inputRotateAxisX;
  155. // Weird clamping code due to weird Euler angle mapping...
  156. float newRotationX = (rotationX - inputRotateAxisY);
  157. if (rotationX <= 90.0f && newRotationX >= 0.0f)
  158. newRotationX = Mathf.Clamp(newRotationX, 0.0f, 90.0f);
  159. if (rotationX >= 270.0f)
  160. newRotationX = Mathf.Clamp(newRotationX, 270.0f, 360.0f);
  161. transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);
  162. float moveSpeed = Time.deltaTime * m_MoveSpeed;
  163. if (fire1 || leftShiftBoost && leftShift)
  164. moveSpeed *= m_Turbo;
  165. transform.position += transform.forward * moveSpeed * inputVertical;
  166. transform.position += transform.right * moveSpeed * inputHorizontal;
  167. transform.position += Vector3.up * moveSpeed * inputYAxis;
  168. }
  169. }
  170. }
  171. }