Advertisement
gandalfbialy

Untitled

May 17th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5. public float jumpForce;
  6. public float liftingForce;
  7.  
  8. public bool jumped;
  9. public bool doubleJumped;
  10.  
  11. public LayerMask whatIsGround;
  12.  
  13. private Rigidbody2D rb;
  14. private BoxCollider2D boxCollider2D;
  15. private float timestamp;
  16.  
  17. void Start()
  18. {
  19. rb = GetComponent<Rigidbody2D>();
  20. boxCollider2D = GetComponent<BoxCollider2D>();
  21. }
  22.  
  23. void Update()
  24. {
  25. if (IsGrounded() && Time.time >= timestamp)
  26. {
  27. if (jumped || doubleJumped)
  28. {
  29. jumped = false;
  30. doubleJumped = false;
  31. }
  32.  
  33. timestamp = Time.time + 0.5f;
  34. }
  35.  
  36. if (Input.GetMouseButtonDown(0))
  37. {
  38. if (!jumped)
  39. {
  40. rb.linearVelocity = (new Vector2(0f, jumpForce));
  41. jumped = true;
  42. }
  43. else if (!doubleJumped)
  44. {
  45. rb.linearVelocity = (new Vector2(0f, jumpForce));
  46. doubleJumped = true;
  47. }
  48. }
  49.  
  50. if (Input.GetMouseButton(0) && rb.linearVelocity.y <= 0f)
  51. {
  52. Debug.Log("Podnoszenie");
  53. rb.AddForce(new Vector2(0f, liftingForce * Time.deltaTime));
  54. }
  55. }
  56.  
  57. private bool IsGrounded()
  58. {
  59. RaycastHit2D hit = Physics2D.BoxCast(
  60. boxCollider2D.bounds.center,
  61. boxCollider2D.bounds.size,
  62. 0f,
  63. Vector2.down,
  64. 0.1f,
  65. whatIsGround
  66. );
  67.  
  68. return hit.collider != null;
  69. }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement