Advertisement
Guest User

InputHandler.cs

a guest
Mar 5th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. using UnityEngine;
  2. public class InputHandler : MonoBehaviour
  3. {
  4. float horizontal;
  5. float vertical;
  6. bool jump;
  7. float lastJumpTime;
  8. bool isJumping;
  9. bool attack;
  10.  
  11. public float maxJumpDuration = 0.2f;
  12.  
  13. public float GetVerticalAxis()
  14. {
  15. return vertical;
  16. }
  17. public float GetHorizontalAxis()
  18. {
  19. return horizontal;
  20. }
  21. public bool GetJumpButtonDown()
  22. {
  23. return jump;
  24. }
  25. public bool GetAttackButtonDown()
  26. {
  27. return attack;
  28. }
  29.  
  30. void Update()
  31. {
  32. horizontal = Input.GetAxisRaw("Horizontal");
  33. vertical = Input.GetAxisRaw("Vertical");
  34. attack = Input.GetButtonDown("Attack");
  35.  
  36. if (!jump && !isJumping && Input.GetButton("Jump"))
  37. {
  38. jump = true;
  39. lastJumpTime = Time.time;
  40. isJumping = true;
  41. }
  42. else if (!Input.GetButton("Jump"))
  43. {
  44. jump = false;
  45. isJumping = false;
  46. }
  47. if (jump && Time.time > lastJumpTime + maxJumpDuration)
  48. {
  49. jump = false;
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement