Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RectangleController : MonoBehaviour
  6. {
  7.  
  8. public float speed;
  9. public float jumpForce;
  10. private float moveInput;
  11.  
  12. private Rigidbody2D rb;
  13.  
  14. private bool isGrounded;
  15. public Transform groundCheck;
  16. //public float checkRadius;
  17. //public LayerMask whatIsGround;
  18.  
  19. private float jumpTimeCounter;
  20. public float jumpTime;
  21. private bool isJumping;
  22. private bool canMove = true;
  23.  
  24.  
  25. private int extraJumps;
  26. public int extraJumpsValue;
  27.  
  28.  
  29. void Start()
  30. {
  31. extraJumps = extraJumpsValue;
  32. rb = GetComponent<Rigidbody2D>();
  33. }
  34.  
  35. void FixedUpdate()
  36. {
  37. if (Input.GetKey(KeyCode.Space))
  38. {
  39. if (canMove)
  40. {
  41. canMove = false;
  42. }
  43. else
  44. {
  45. canMove = true;
  46. }
  47. }
  48. if (canMove)
  49. {
  50.  
  51. //isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  52.  
  53. moveInput = Input.GetAxis("Horizontal");
  54. rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  55. }
  56. }
  57.  
  58. void Update()
  59. {
  60. if (Input.GetKey(KeyCode.Space))
  61. {
  62. if (canMove)
  63. {
  64. canMove = false;
  65. }
  66. else
  67. {
  68. canMove = true;
  69. }
  70. }
  71.  
  72. if (canMove)
  73. {
  74.  
  75. if (isGrounded == true)
  76. {
  77. extraJumps = extraJumpsValue;
  78. }
  79.  
  80. if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
  81. {
  82. isJumping = true;
  83. jumpTimeCounter = jumpTime;
  84. rb.velocity = Vector2.up * jumpForce;
  85. extraJumps--;
  86.  
  87. }
  88. else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
  89. {
  90. isJumping = true;
  91. jumpTimeCounter = jumpTime;
  92. rb.velocity = Vector2.up * jumpForce;
  93. }
  94.  
  95. if (Input.GetKey(KeyCode.UpArrow) && isJumping == true)
  96. {
  97.  
  98. if (jumpTimeCounter > 0)
  99. {
  100. rb.velocity = Vector2.up * jumpForce;
  101. jumpTimeCounter -= Time.deltaTime;
  102. }
  103. else
  104. {
  105. isJumping = false;
  106. }
  107.  
  108. }
  109. }
  110. }
  111.  
  112. private void OnTriggerEnter(Collider other)
  113. {
  114. if (other.gameObject.tag == "Ground" && canMove)
  115. {
  116. isGrounded = true;
  117. }
  118. }
  119.  
  120.  
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement