Création d'un petit party-game anonyme de Build + Bagar sous Unity
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FreeCam.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class FreeCam : MonoBehaviour
  4. {
  5. [Header("Controls")]
  6. public float moveSpeed = 10f;
  7. public float sprintSpeed = 30f;
  8. public bool ghost = true;
  9. [Header("Sensitivity")]
  10. public float XAxisSensitivity = 30f;
  11. public float YAxisSensitivity = 30f;
  12. [Space]
  13. [Range(0, 89)] public float MaxXAngle = 60f;
  14. // privates
  15. private Vector2 _inputMouse;
  16. private Vector2 _velocity;
  17. private CharacterController _controller;
  18. private BlockSpawner _blockSpawner;
  19. private float _rotationX;
  20. private bool _isSprinting;
  21. private void Start()
  22. {
  23. // TODO remove - doesn't belong here - test only
  24. Cursor.lockState = CursorLockMode.Locked;
  25. Cursor.visible = false;
  26. _inputMouse = Vector2.zero;
  27. _velocity = Vector2.zero;
  28. _controller = gameObject.GetComponent<CharacterController>();
  29. _blockSpawner = gameObject.GetComponent<BlockSpawner>();
  30. _rotationX = 0;
  31. _isSprinting = false;
  32. }
  33. // Update is called once per frame
  34. // TODO test if checking (input values = 0) to avoid calculus reduces CPU usage
  35. private void Update()
  36. {
  37. float rotationHorizontal = _inputMouse.x * XAxisSensitivity * Time.deltaTime;
  38. float rotationVertical = _inputMouse.y * YAxisSensitivity * Time.deltaTime;
  39. // always rotate Y in global world space to avoid gimbal lock
  40. transform.Rotate(Vector3.up * rotationHorizontal, Space.World);
  41. float rotationY = transform.localEulerAngles.y;
  42. _rotationX += rotationVertical;
  43. _rotationX = Mathf.Clamp(_rotationX, -MaxXAngle, MaxXAngle);
  44. transform.localEulerAngles = new Vector3(-_rotationX, rotationY, 0);
  45. float speed = _isSprinting ? sprintSpeed : moveSpeed;
  46. float translationX = _velocity.x * speed * Time.deltaTime;
  47. float translationZ = _velocity.y * speed * Time.deltaTime;
  48. Vector3 _move = new Vector3(translationX, 0f, translationZ);
  49. // ghost directly moves transform. non-ghost uses CharacterController collision detection
  50. if (ghost) {
  51. transform.Translate(_move);
  52. } else {
  53. _move = transform.TransformVector(_move);
  54. _controller.Move(_move);
  55. }
  56. }
  57. public void OnLook(InputValue value)
  58. {
  59. Vector2 valueAsVector2 = value.Get<Vector2>();
  60. float xVelocity = valueAsVector2.x;
  61. float yVelocity = valueAsVector2.y;
  62. if (_blockSpawner._blockRotating)
  63. {
  64. xVelocity *= 0.05f;
  65. yVelocity *= 0.05f;
  66. }
  67. _inputMouse.x = xVelocity;
  68. _inputMouse.y = yVelocity;
  69. }
  70. public void OnMovement(InputValue value)
  71. {
  72. Vector2 valueAsVector2 = value.Get<Vector2>();
  73. _velocity.x = valueAsVector2.x;
  74. _velocity.y = valueAsVector2.y;
  75. }
  76. public void OnMovementBlockRotating(InputValue value)
  77. {
  78. OnMovement(value);
  79. }
  80. public void OnSprint(InputValue value)
  81. {
  82. float valueAsFloat = value.Get<float>();
  83. _isSprinting = valueAsFloat == 1f;
  84. }
  85. }