Advertisement
gandalfbialy

Untitled

Jul 11th, 2025
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.     public bool jumped;
  6.     public bool doubleJumped;
  7.  
  8.     public float jumpForce;
  9.     public float liftingForce;
  10.  
  11.     private Rigidbody2D rb;
  12.     private float timestamp;
  13.     private BoxCollider2D boxCollider2D;
  14.  
  15.     public LayerMask whatIsGround;
  16.    
  17.     void Start()
  18.     {
  19.         rb = gameObject.GetComponent<Rigidbody2D>();
  20.         boxCollider2D = gameObject.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 + 1f;
  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 < 0)
  51.         {
  52.             rb.AddForce(new Vector2(0f, liftingForce * Time.deltaTime));
  53.         }
  54.     }
  55.  
  56.     private bool IsGrounded()
  57.     {
  58.         RaycastHit2D hit = Physics2D.BoxCast(boxCollider2D.bounds.center, boxCollider2D.bounds.size, 0f, Vector2.down, 0.1f, whatIsGround);
  59.  
  60.         return hit.collider != null;
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement