Advertisement
Guest User

playerctrl

a guest
Oct 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class PlayerCtrl : MonoBehaviour
  7. {
  8. [Tooltip("this is an positive integer which speeds up the player movement")]
  9. public int speedBoost; // set this to 5
  10. public float jumpSpeed; // set this to 600
  11. public bool isGrounded;
  12. public Transform feet;
  13. public float feetRadius;
  14. public LayerMask whatIsGround;
  15. public Transform leftBulletSpawnPos, rightBulletSpawnPos;
  16. public GameObject leftBullet, rightBullet;
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. Rigidbody2D rb;
  24. SpriteRenderer sr;
  25. Animator anim;
  26. bool isJumping;
  27. bool leftPressed, rightPressed;
  28.  
  29.  
  30.  
  31.  
  32. void Start()
  33. {
  34. rb = GetComponent<Rigidbody2D>();
  35. sr = GetComponent<SpriteRenderer>();
  36. anim = GetComponent<Animator>();
  37.  
  38. }
  39.  
  40.  
  41.  
  42.  
  43. void Update()
  44. {
  45.  
  46.  
  47. isGrounded = Physics2D.OverlapCircle(feet.position, feetRadius, whatIsGround);
  48.  
  49. float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
  50. playerSpeed *= speedBoost; // playerSpeed = playerSpeed * speedBoost
  51. if (playerSpeed != 0)
  52. MoveHorizontal(playerSpeed);
  53. else
  54. StopMoving();
  55.  
  56. if (Input.GetButtonDown("Jump"))
  57. Jump();
  58.  
  59. if (Input.GetButtonDown("Fire1"))
  60. {
  61. FireBullets();
  62. }
  63.  
  64.  
  65.  
  66. ShowFalling();
  67.  
  68. if (leftPressed)
  69. MoveHorizontal(-speedBoost);
  70.  
  71. if (rightPressed)
  72. MoveHorizontal(speedBoost);
  73.  
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. void OnDrawGizmos()
  84. {
  85. Gizmos.DrawWireSphere(feet.position, feetRadius);
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void MoveHorizontal(float playerSpeed)
  96. {
  97. rb.velocity = new Vector2(playerSpeed, rb.velocity.y);
  98. if (playerSpeed < 0)
  99. sr.flipX = true;
  100. else if (playerSpeed > 0)
  101. sr.flipX = false;
  102. if (!isJumping)
  103. anim.SetInteger("State", 1);
  104. }
  105.  
  106. void StopMoving()
  107. {
  108. rb.velocity = new Vector2(0, rb.velocity.y);
  109.  
  110. if (!isJumping)
  111. anim.SetInteger("State", 0);
  112.  
  113. }
  114. void ShowFalling()
  115. {
  116. if (rb.velocity.y < 0)
  117. {
  118. anim.SetInteger("State", 3);
  119. }
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. void Jump()
  131. {
  132. if (isGrounded)
  133. {
  134. isJumping = true;
  135. rb.AddForce(new Vector2(0, jumpSpeed)); //simply make the player jump in the y axis or upwards
  136. anim.SetInteger("State", 2);
  137. }
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. void FireBullets()
  153. {
  154.  
  155. // makes the player fire bullets in the left direction
  156. if (sr.flipX)
  157. {
  158. Instantiate(leftBullet, leftBulletSpawnPos.position, Quaternion.identity);
  159. }
  160.  
  161.  
  162. // makes the player fire bullets in the right direction
  163. if (!sr.flipX)
  164. {
  165. Instantiate(rightBullet, rightBulletSpawnPos.position, Quaternion.identity);
  166. }
  167. }
  168.  
  169. public void mobileMoveLeft()
  170. {
  171. leftPressed = true
  172. }
  173.  
  174. public void mobileMoveright()
  175. {
  176. rightPressed = true
  177. }
  178.  
  179.  
  180. public void MobileStop()
  181. {
  182. leftPressed = false
  183. rightPressed = false
  184. StopMoving();
  185. }
  186.  
  187.  
  188. void OnCollisionEnter2D(Collision2D other)
  189. {
  190. if (other.gameObject.CompareTag("Ground"))
  191. {
  192. isJumping = false;
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement