SimpleCameraController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #if ENABLE_INPUT_SYSTEM
  2. using UnityEngine.InputSystem;
  3. #endif
  4. using UnityEngine;
  5. namespace UnityTemplateProjects
  6. {
  7. public class SimpleCameraController : MonoBehaviour
  8. {
  9. class CameraState
  10. {
  11. public float yaw;
  12. public float pitch;
  13. public float roll;
  14. public float x;
  15. public float y;
  16. public float z;
  17. public void SetFromTransform(Transform t)
  18. {
  19. pitch = t.eulerAngles.x;
  20. yaw = t.eulerAngles.y;
  21. roll = t.eulerAngles.z;
  22. x = t.position.x;
  23. y = t.position.y;
  24. z = t.position.z;
  25. }
  26. public void Translate(Vector3 translation)
  27. {
  28. Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
  29. x += rotatedTranslation.x;
  30. y += rotatedTranslation.y;
  31. z += rotatedTranslation.z;
  32. }
  33. public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
  34. {
  35. yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
  36. pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
  37. roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
  38. x = Mathf.Lerp(x, target.x, positionLerpPct);
  39. y = Mathf.Lerp(y, target.y, positionLerpPct);
  40. z = Mathf.Lerp(z, target.z, positionLerpPct);
  41. }
  42. public void UpdateTransform(Transform t)
  43. {
  44. t.eulerAngles = new Vector3(pitch, yaw, roll);
  45. t.position = new Vector3(x, y, z);
  46. }
  47. }
  48. public static int smallclickct =5;
  49. const float k_MouseSensitivityMultiplier = 0.01f;
  50. CameraState m_TargetCameraState = new CameraState();
  51. CameraState m_InterpolatingCameraState = new CameraState();
  52. [Header("Movement Settings")]
  53. [Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
  54. public float boost = 5f;
  55. [Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
  56. public float positionLerpTime = 1f;
  57. [Header("Rotation Settings")]
  58. [Tooltip("Multiplier for the sensitivity of the rotation.")]
  59. public float mouseSensitivity = 120f;
  60. [Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
  61. public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
  62. [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
  63. public float rotationLerpTime = 1f;
  64. [Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
  65. public bool invertY = false;
  66. public float fwordSpeed = 200f;
  67. #if ENABLE_INPUT_SYSTEM
  68. InputAction movementAction;
  69. InputAction verticalMovementAction;
  70. InputAction lookAction;
  71. InputAction boostFactorAction;
  72. bool mouseRightButtonPressed;
  73. void Start()
  74. {
  75. var map = new InputActionMap("Simple Camera Controller");
  76. lookAction = map.AddAction("look", binding: "<Mouse>/delta");
  77. movementAction = map.AddAction("move", binding: "<Gamepad>/leftStick");
  78. verticalMovementAction = map.AddAction("Vertical Movement");
  79. boostFactorAction = map.AddAction("Boost Factor", binding: "<Mouse>/scroll");
  80. lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
  81. movementAction.AddCompositeBinding("Dpad")
  82. .With("Up", "<Keyboard>/w")
  83. .With("Up", "<Keyboard>/upArrow")
  84. .With("Down", "<Keyboard>/s")
  85. .With("Down", "<Keyboard>/downArrow")
  86. .With("Left", "<Keyboard>/a")
  87. .With("Left", "<Keyboard>/leftArrow")
  88. .With("Right", "<Keyboard>/d")
  89. .With("Right", "<Keyboard>/rightArrow");
  90. verticalMovementAction.AddCompositeBinding("Dpad")
  91. .With("Up", "<Keyboard>/pageUp")
  92. .With("Down", "<Keyboard>/pageDown")
  93. .With("Up", "<Keyboard>/e")
  94. .With("Down", "<Keyboard>/q")
  95. .With("Up", "<Gamepad>/rightshoulder")
  96. .With("Down", "<Gamepad>/leftshoulder");
  97. boostFactorAction.AddBinding("<Gamepad>/Dpad").WithProcessor("scaleVector2(x=1, y=4)");
  98. movementAction.Enable();
  99. lookAction.Enable();
  100. verticalMovementAction.Enable();
  101. boostFactorAction.Enable();
  102. }
  103. #endif
  104. public void initpos()
  105. {
  106. m_TargetCameraState.SetFromTransform(transform);
  107. m_InterpolatingCameraState.SetFromTransform(transform);
  108. }
  109. void OnEnable()
  110. {
  111. m_TargetCameraState.SetFromTransform(transform);
  112. m_InterpolatingCameraState.SetFromTransform(transform);
  113. }
  114. Vector3 GetInputTranslationDirection()
  115. {
  116. Vector3 direction = Vector3.zero;
  117. #if ENABLE_INPUT_SYSTEM
  118. var moveDelta = movementAction.ReadValue<Vector2>();
  119. direction.x = moveDelta.x;
  120. direction.z = moveDelta.y;
  121. direction.y = verticalMovementAction.ReadValue<Vector2>().y;
  122. #else
  123. if(Input.GetMouseButton(0) )
  124. {
  125. direction += Vector3.down * Input.GetAxis("Mouse Y");
  126. direction += Vector3.left * Input.GetAxis("Mouse X");
  127. }
  128. direction += Vector3.forward * GetBoostFactor()* fwordSpeed*Mathf.Abs(this.transform.localPosition.y)/50f;
  129. //if (Input.GetKey(KeyCode.W))
  130. //{
  131. // direction += Vector3.forward;
  132. //}
  133. //if (Input.GetKey(KeyCode.S))
  134. //{
  135. // direction += Vector3.back;
  136. //}
  137. //if (Input.GetKey(KeyCode.A))
  138. //{
  139. // direction += Vector3.left;
  140. //}
  141. //if (Input.GetKey(KeyCode.D))
  142. //{
  143. // direction += Vector3.right;
  144. //}
  145. //if (Input.GetKey(KeyCode.Q))
  146. //{
  147. // direction += Vector3.down;
  148. //}
  149. //if (Input.GetKey(KeyCode.E))
  150. //{
  151. // direction += Vector3.up;
  152. //}
  153. #endif
  154. return direction;
  155. }
  156. bool islst;
  157. bool gunlun;
  158. public void enter()
  159. {
  160. islst=true;
  161. gunlun=true;
  162. }
  163. public void exit()
  164. {
  165. gunlun=false;
  166. }
  167. public void up()
  168. {
  169. islst=false;
  170. }
  171. void Update()
  172. {
  173. if(!islst&&!gunlun)
  174. {
  175. return;
  176. }
  177. // Exit Sample
  178. if (IsEscapePressed())
  179. {
  180. Application.Quit();
  181. #if UNITY_EDITOR
  182. UnityEditor.EditorApplication.isPlaying = false;
  183. #endif
  184. }
  185. // Hide and lock cursor when right mouse button pressed
  186. if (IsRightMouseButtonDown())
  187. {
  188. boost=smallclickct;
  189. //Cursor.lockState = CursorLockMode.Locked;
  190. }
  191. // Unlock and show cursor when right mouse button released
  192. if (IsRightMouseButtonUp())
  193. {
  194. boost=3;
  195. // Cursor.visible = true;
  196. // Cursor.lockState = CursorLockMode.None;
  197. }
  198. // Rotation
  199. if (IsCameraRotationAllowed())
  200. {
  201. var mouseMovement = GetInputLookRotation() * k_MouseSensitivityMultiplier * mouseSensitivity;
  202. if (invertY)
  203. mouseMovement.y = -mouseMovement.y;
  204. var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
  205. m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
  206. m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
  207. }
  208. // Translation
  209. var translation = GetInputTranslationDirection() * Time.deltaTime;
  210. // Speed up movement when shift key held
  211. if (IsBoostPressed())
  212. {
  213. translation *= 10.0f;
  214. }
  215. // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
  216. //boost += GetBoostFactor();
  217. float vb = Mathf.Abs(this.transform.localPosition.y)/200f;
  218. if(vb>3)
  219. {
  220. vb =3;
  221. }
  222. translation *= Mathf.Pow(2.0f, boost+vb);
  223. m_TargetCameraState.Translate(translation);
  224. // Framerate-independent interpolation
  225. // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
  226. var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
  227. var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
  228. m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
  229. m_InterpolatingCameraState.UpdateTransform(transform);
  230. }
  231. /// <summary>
  232. /// ����м�����WASD�ƶ��ٶ�
  233. /// </summary>
  234. /// <returns></returns>
  235. float GetBoostFactor()
  236. {
  237. if(!gunlun)
  238. {
  239. return 0;
  240. }
  241. #if ENABLE_INPUT_SYSTEM
  242. return boostFactorAction.ReadValue<Vector2>().y * 0.01f;
  243. #else
  244. return Input.mouseScrollDelta.y * 0.02f;
  245. #endif
  246. }
  247. /// <summary>
  248. /// ��ȡ���XY����ת
  249. /// </summary>
  250. /// <returns></returns>
  251. Vector2 GetInputLookRotation()
  252. {
  253. if(!islst)
  254. {
  255. return Vector2.zero;
  256. }
  257. // try to compensate the diff between the two input systems by multiplying with empirical values
  258. #if ENABLE_INPUT_SYSTEM
  259. var delta = lookAction.ReadValue<Vector2>();
  260. delta *= 0.5f; // Account for scaling applied directly in Windows code by old input system.
  261. delta *= 0.1f; // Account for sensitivity setting on old Mouse X and Y axes.
  262. return delta;
  263. #else
  264. return new Vector2(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
  265. #endif
  266. }
  267. bool IsBoostPressed()
  268. {
  269. #if ENABLE_INPUT_SYSTEM
  270. bool boost = Keyboard.current != null ? Keyboard.current.leftShiftKey.isPressed : false;
  271. boost |= Gamepad.current != null ? Gamepad.current.xButton.isPressed : false;
  272. return boost;
  273. #else
  274. return Input.GetKey(KeyCode.LeftShift);
  275. #endif
  276. }
  277. bool IsEscapePressed()
  278. {
  279. #if ENABLE_INPUT_SYSTEM
  280. return Keyboard.current != null ? Keyboard.current.escapeKey.isPressed : false;
  281. #else
  282. return Input.GetKey(KeyCode.Escape);
  283. #endif
  284. }
  285. bool IsCameraRotationAllowed()
  286. {
  287. #if ENABLE_INPUT_SYSTEM
  288. bool canRotate = Mouse.current != null ? Mouse.current.rightButton.isPressed : false;
  289. canRotate |= Gamepad.current != null ? Gamepad.current.rightStick.ReadValue().magnitude > 0 : false;
  290. return canRotate;
  291. #else
  292. return Input.GetMouseButton(1);
  293. #endif
  294. }
  295. bool IsRightMouseButtonDown()
  296. {
  297. #if ENABLE_INPUT_SYSTEM
  298. return Mouse.current != null ? Mouse.current.rightButton.isPressed : false;
  299. #else
  300. return Input.GetMouseButtonDown(0);
  301. #endif
  302. }
  303. bool IsRightMouseButtonUp()
  304. {
  305. #if ENABLE_INPUT_SYSTEM
  306. return Mouse.current != null ? !Mouse.current.rightButton.isPressed : false;
  307. #else
  308. return Input.GetMouseButtonUp(0);
  309. #endif
  310. }
  311. }
  312. }