Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public float jumpForce;
- public float liftingForce;
- public bool jumped;
- public bool doubleJumped;
- public LayerMask whatIsGround;
- private Rigidbody2D rb;
- private BoxCollider2D boxCollider2D;
- private float timestamp;
- void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- boxCollider2D = GetComponent<BoxCollider2D>();
- }
- void Update()
- {
- if (IsGrounded() && Time.time >= timestamp)
- {
- if (jumped || doubleJumped)
- {
- jumped = false;
- doubleJumped = false;
- }
- timestamp = Time.time + 0.5f;
- }
- if (Input.GetMouseButtonDown(0))
- {
- if (!jumped)
- {
- rb.linearVelocity = (new Vector2(0f, jumpForce));
- jumped = true;
- }
- else if (!doubleJumped)
- {
- rb.linearVelocity = (new Vector2(0f, jumpForce));
- doubleJumped = true;
- }
- }
- if (Input.GetMouseButton(0) && rb.linearVelocity.y <= 0f)
- {
- Debug.Log("Podnoszenie");
- rb.AddForce(new Vector2(0f, liftingForce * Time.deltaTime));
- }
- }
- private bool IsGrounded()
- {
- RaycastHit2D hit = Physics2D.BoxCast(
- boxCollider2D.bounds.center,
- boxCollider2D.bounds.size,
- 0f,
- Vector2.down,
- 0.1f,
- whatIsGround
- );
- return hit.collider != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement