Advertisement
Guest User

InputManager

a guest
Jun 17th, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine.InputSystem.Interactions;
  4. public class InputManager : MonoBehaviour
  5. {
  6. [SerializeField] private PlayerInput PlayerInput;
  7. public Vector2 Move { get; private set; }
  8. public Vector2 Look { get; private set; }
  9. public bool Run { get; private set; }
  10. public bool Jump { get; private set; }
  11. public bool Crouch { get; private set; }
  12. public bool Fire { get; private set; }
  13. public bool Aim { get; private set; }
  14. public bool SwitchCamera { get; set; }
  15.  
  16. public Vector2 ZoomLook { get; private set; }
  17.  
  18. private InputActionMap _currentMap;
  19.  
  20. private InputAction _moveAction;
  21.  
  22. private InputAction _lookAction;
  23.  
  24. private InputAction _runAction;
  25.  
  26. private InputAction _jumpAction;
  27.  
  28. private InputAction _crouchAction;
  29.  
  30. private InputAction _fireAction;
  31.  
  32. private InputAction _aimAction;
  33.  
  34. private InputAction _switchCameraAction;
  35.  
  36. private InputAction _zoomLookAction;
  37.  
  38. private void Awake()
  39. {
  40. HideCursor();
  41. _currentMap = PlayerInput.currentActionMap;
  42. _moveAction = _currentMap.FindAction("Move");
  43. _lookAction = _currentMap.FindAction("Look");
  44. _runAction = _currentMap.FindAction("Run");
  45. _jumpAction = _currentMap.FindAction("Jump");
  46. _crouchAction = _currentMap.FindAction("Crouch");
  47. _fireAction = _currentMap.FindAction("Fire");
  48. _aimAction = _currentMap.FindAction("Aim");
  49. _switchCameraAction = _currentMap.FindAction("SwitchCamera");
  50. _zoomLookAction = _currentMap.FindAction("ZoomLook");
  51.  
  52. _moveAction.performed += onMove;
  53. _lookAction.performed += onLook;
  54. _runAction.performed += onRun;
  55. _jumpAction.performed += onJump;
  56. _crouchAction.performed += onCrouch;
  57. _fireAction.performed += onFire;
  58. _aimAction.performed += onAim;
  59. _switchCameraAction.performed += onSwitchCamera;
  60. _zoomLookAction.performed += onZoomLook;
  61.  
  62. _moveAction.canceled += onMove;
  63. _lookAction.canceled += onLook;
  64. _runAction.canceled += onRun;
  65. _jumpAction.canceled += onJump;
  66. _crouchAction.canceled += onCrouch;
  67. _fireAction.canceled += onFire;
  68. _aimAction.canceled += onAim;
  69. //_switchCameraAction.canceled += onSwitchCamera;
  70. _zoomLookAction.canceled += onZoomLook;
  71. }
  72.  
  73. private void HideCursor()
  74. {
  75. Cursor.visible = false;
  76. Cursor.lockState = CursorLockMode.Locked;
  77. }
  78.  
  79. private void onMove(InputAction.CallbackContext context)
  80. {
  81. Move = context.ReadValue<Vector2>();
  82. }
  83.  
  84. private void onLook(InputAction.CallbackContext context)
  85. {
  86. Look = context.ReadValue<Vector2>();
  87. }
  88.  
  89. private void onRun(InputAction.CallbackContext context)
  90. {
  91. Run = context.ReadValueAsButton();
  92. }
  93.  
  94. private void onJump(InputAction.CallbackContext context)
  95. {
  96. Jump = context.ReadValueAsButton();
  97. }
  98.  
  99. private void onCrouch(InputAction.CallbackContext context)
  100. {
  101. Crouch = context.ReadValueAsButton();
  102. }
  103.  
  104. private void onFire(InputAction.CallbackContext context)
  105. {
  106. Fire = context.ReadValueAsButton();
  107. }
  108.  
  109. private void onAim(InputAction.CallbackContext context)
  110. {
  111. Aim = context.ReadValueAsButton();
  112. }
  113.  
  114. private void onSwitchCamera(InputAction.CallbackContext context)
  115. {
  116. SwitchCamera = !SwitchCamera;
  117. Debug.Log(context.phase);
  118. }
  119.  
  120. private void onZoomLook(InputAction.CallbackContext context)
  121. {
  122. ZoomLook = context.ReadValue<Vector2>().normalized;
  123. }
  124.  
  125. private void OnEnable()
  126. {
  127. _currentMap.Enable();
  128. }
  129.  
  130. private void OnDisable()
  131. {
  132. _currentMap.Disable();
  133. }
  134. }
  135.  
  136.  
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement