Peach_88

Input Example

Dec 22nd, 2025 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. namespace GameFlight
  6. {
  7. public class Inputs : MonoBehaviour
  8. {
  9. public PlayerInput _playerInputs;
  10. public bool LeftMousePressed => Mouse.current.leftButton.wasPressedThisFrame;
  11. public bool RightMousePressed => Mouse.current.rightButton.wasPressedThisFrame;
  12. void OnEnable() => _playerInputs.onActionTriggered += OnActionTriggered;
  13. void OnActionTriggered(InputAction.CallbackContext ctx)
  14. {
  15.  
  16. }
  17.  
  18. void OnDisable() => _playerInputs.onActionTriggered -= OnActionTriggered;
  19.  
  20. public bool Pressed(string action) => _playerInputs.actions[action].WasPerformedThisFrame();
  21. public bool Held(string action) => _playerInputs.actions[action].IsPressed();
  22. public bool Released(string action) => _playerInputs.actions[action].WasReleasedThisFrame();
  23. public float Value(string action) => _playerInputs.actions[action].ReadValue<float>();
  24. public Vector2 Axis2D(string action) => _playerInputs.actions[action].ReadValue<Vector2>();
  25. }
  26. }
  27.  
  28. using System;
  29. using UnityEngine;
  30.  
  31. namespace GameFlight
  32. {
  33. public class Player : MonoBehaviour
  34. {
  35. public CharacterCamera CharacterCamera;
  36. public Transform CameraFollowPoint;
  37. public Character Character;
  38.  
  39. Inputs _inputs;
  40.  
  41. void Awake()
  42. {
  43. _inputs = App.Get<Inputs>();
  44. Cursor.lockState = CursorLockMode.Locked;
  45. }
  46.  
  47. private void Start()
  48. {
  49. CharacterCamera.SetFollowTransform(CameraFollowPoint);
  50. CharacterCamera.IgnoredColliders.AddRange(Character.GetComponentsInChildren<Collider>());
  51. }
  52.  
  53. private void Update()
  54. {
  55. if (_inputs.LeftMousePressed) Cursor.lockState = CursorLockMode.Locked;
  56.  
  57. HandleCharacterInput();
  58. }
  59.  
  60. private void HandleCharacterInput()
  61. {
  62. if (!Character) return;
  63.  
  64. Vector3 move = _inputs.Axis2D(InputActionNames.Move);
  65.  
  66. CharInputs characterInputs = new CharInputs
  67. {
  68. MoveAxisForward = move.y,
  69. MoveAxisRight = move.x,
  70. CameraRotation = CharacterCamera.Transform.rotation,
  71. JumpDown = _inputs.Pressed(InputActionNames.Jump),
  72. CrouchDown = _inputs.Pressed(InputActionNames.Crouch),
  73. CrouchUp = _inputs.Released(InputActionNames.Crouch),
  74. Interact = _inputs.Pressed(InputActionNames.Interact),
  75. };
  76.  
  77. Character.SetInputs(ref characterInputs);
  78. }
  79. }
  80. }
  81.  
  82. using UnityEngine;
  83.  
  84. namespace GameFlight
  85. {
  86. public static class InputActionNames
  87. {
  88. public static readonly string CameraMovement = "CameraMovement";
  89. public static readonly string Look = "Look";
  90. public static readonly string Move = "Move";
  91. public static readonly string Zoom = "Zoom";
  92. public static readonly string Jump = "Jump";
  93. public static readonly string Crouch = "Crouch";
  94. public static readonly string Interact = "Interact";
  95. }
  96. }
  97.  
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment