Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RectangleController : MonoBehaviour
- {
- public float speed;
- public float jumpForce;
- private float moveInput;
- private Rigidbody2D rb;
- private bool isGrounded;
- public Transform groundCheck;
- //public float checkRadius;
- //public LayerMask whatIsGround;
- private float jumpTimeCounter;
- public float jumpTime;
- private bool isJumping;
- private bool canMove = true;
- private int extraJumps;
- public int extraJumpsValue;
- void Start()
- {
- extraJumps = extraJumpsValue;
- rb = GetComponent<Rigidbody2D>();
- }
- void FixedUpdate()
- {
- if (Input.GetKey(KeyCode.Space))
- {
- if (canMove)
- {
- canMove = false;
- }
- else
- {
- canMove = true;
- }
- }
- if (canMove)
- {
- //isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
- moveInput = Input.GetAxis("Horizontal");
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- }
- }
- void Update()
- {
- if (Input.GetKey(KeyCode.Space))
- {
- if (canMove)
- {
- canMove = false;
- }
- else
- {
- canMove = true;
- }
- }
- if (canMove)
- {
- if (isGrounded == true)
- {
- extraJumps = extraJumpsValue;
- }
- if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
- {
- isJumping = true;
- jumpTimeCounter = jumpTime;
- rb.velocity = Vector2.up * jumpForce;
- extraJumps--;
- }
- else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
- {
- isJumping = true;
- jumpTimeCounter = jumpTime;
- rb.velocity = Vector2.up * jumpForce;
- }
- if (Input.GetKey(KeyCode.UpArrow) && isJumping == true)
- {
- if (jumpTimeCounter > 0)
- {
- rb.velocity = Vector2.up * jumpForce;
- jumpTimeCounter -= Time.deltaTime;
- }
- else
- {
- isJumping = false;
- }
- }
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.tag == "Ground" && canMove)
- {
- isGrounded = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement