Advertisement
Guest User

Untitled

a guest
Oct 1st, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class Player : MonoBehaviour{
  5. //FOR JUMPING https://www.youtube.com/watch?v=QGDeafTx5ug
  6.  
  7. public GameObject losePanel;
  8.  
  9. public Text healthDisplay;
  10.  
  11. public float speed;
  12. private float input;
  13. public int health;
  14. //public float jumpForce;
  15.  
  16. private Rigidbody2D rb; //varaible lets use of physics
  17. Animator anim;
  18. AudioSource source;
  19.  
  20. private bool facingRight = true;
  21.  
  22. public float startDashTime;
  23. private float dashTime;
  24. public float extraSpeed;
  25. private bool isDashing;
  26.  
  27. //jumping
  28. /*private bool isGrounded;
  29. //Used to make circle hit box to check if player is on ground
  30. public Transform groundCheck;
  31. public float checkRadius;
  32. public LayerMask WhatIsGround;
  33.  
  34. private int extraJumps;
  35. public int extraJumpsValue;*/
  36.  
  37. void Start () {
  38. /*extraJumps = extraJumpsValue;*/
  39. anim = GetComponent<Animator>();
  40. rb = GetComponent<Rigidbody2D>(); //sets rb to the physics
  41. healthDisplay.text = health.ToString();
  42. source = GetComponent<AudioSource>();
  43. }
  44.  
  45.  
  46. void FixedUpdate () { //When you are working with physics
  47.  
  48. /*isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);*/
  49.  
  50. input = Input.GetAxisRaw("Horizontal"); //Just GetAxis is smooth, Get axis raw is snappy
  51. //Makes player move, vector 2 variable
  52. rb.velocity = new Vector2(input * speed, rb.velocity.y); //input * speed is for x-axis, makes y do 0: rb.velocity.y
  53.  
  54. //Calls flip function depending on if character is moving right or left
  55. if(facingRight == false && input > 0) {
  56. Flip();
  57. } else if(facingRight == true && input < 0) {
  58. Flip();
  59. }
  60. }
  61.  
  62. private void Update() {
  63.  
  64. /*Makes player jump
  65. if(isGrounded == true) {
  66. extraJumps = extraJumpsValue;
  67. }
  68.  
  69. if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
  70. rb.velocity = Vector2.up * jumpForce;
  71. extraJumps--; //DONT FORGET THIS
  72. }
  73. else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true) {
  74. rb.velocity = Vector2.up * jumpForce;
  75. }*/
  76.  
  77. //Choose between running or idle
  78. if (input !=0) {
  79. anim.SetBool("isRunning", true);
  80. }
  81. else {
  82. anim.SetBool("isRunning", false);
  83. }
  84.  
  85. if (Input.GetKeyDown(KeyCode.Space) && isDashing == false) {
  86. speed += extraSpeed;
  87. isDashing = true;
  88. dashTime = startDashTime;
  89. }
  90. if (dashTime <= 0 && isDashing == true) {
  91. isDashing = false;
  92. speed -= extraSpeed;
  93. }
  94. else {
  95. dashTime -= Time.deltaTime;
  96. }
  97.  
  98. }
  99.  
  100. void Flip() {
  101.  
  102. facingRight = !facingRight; //checks if the are right or left
  103. Vector3 Scaler = transform.localScale; //What makes them flip
  104. Scaler.x *= -1;
  105. transform.localScale = Scaler;
  106. }
  107. //Must be public to be accessed in enemy script
  108. public void TakeDamage(int damageAmount) {
  109. source.Play();
  110. health -= damageAmount;
  111. healthDisplay.text = health.ToString();
  112.  
  113. if (health <= 0) {
  114. //Lose Screen
  115. losePanel.SetActive(true);
  116. //Destroy Player
  117. Destroy(gameObject); //destroy the object this script is on
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement