Advertisement
gandalfbialy

Untitled

Jun 21st, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 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 (!GameManager.instance.inGame)
  26. {
  27. return;
  28. }
  29.  
  30. if (IsGrounded() && Time.time >= timestamp)
  31. {
  32. if (jumped || doubleJumped)
  33. {
  34. jumped = false;
  35. doubleJumped = false;
  36. }
  37.  
  38. timestamp = Time.time + 0.5f;
  39. }
  40.  
  41. if (Input.GetMouseButtonDown(0))
  42. {
  43. if (!jumped)
  44. {
  45. rb.linearVelocity = (new Vector2(0f, jumpForce));
  46. jumped = true;
  47. }
  48. else if (!doubleJumped)
  49. {
  50. rb.linearVelocity = (new Vector2(0f, jumpForce));
  51. doubleJumped = true;
  52. }
  53. }
  54.  
  55. if (Input.GetMouseButton(0) && rb.linearVelocity.y <= 0f)
  56. {
  57. Debug.Log("Podnoszenie");
  58. rb.AddForce(new Vector2(0f, liftingForce * Time.deltaTime));
  59. }
  60. }
  61.  
  62. private bool IsGrounded()
  63. {
  64. RaycastHit2D hit = Physics2D.BoxCast(
  65. boxCollider2D.bounds.center,
  66. boxCollider2D.bounds.size,
  67. 0f,
  68. Vector2.down,
  69. 1.0f,
  70. whatIsGround
  71. );
  72.  
  73. return hit.collider != null;
  74. }
  75.  
  76. void OnTriggerEnter2D(Collider2D other)
  77. {
  78. if (other.CompareTag("Obstacle"))
  79. {
  80. PlayerDeath();
  81. }
  82. else if (other.CompareTag("Coin"))
  83. {
  84. Debug.Log("Coin dotknięty");
  85. GameManager.instance.CoinCollected();
  86. Destroy(other.gameObject);
  87. }
  88. }
  89. void PlayerDeath()
  90. {
  91. GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
  92. GameManager.instance.GameOver();
  93. }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement