Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- public class Player : MonoBehaviour{
- //FOR JUMPING https://www.youtube.com/watch?v=QGDeafTx5ug
- public GameObject losePanel;
- public Text healthDisplay;
- public float speed;
- private float input;
- public int health;
- //public float jumpForce;
- private Rigidbody2D rb; //varaible lets use of physics
- Animator anim;
- AudioSource source;
- private bool facingRight = true;
- public float startDashTime;
- private float dashTime;
- public float extraSpeed;
- private bool isDashing;
- //jumping
- /*private bool isGrounded;
- //Used to make circle hit box to check if player is on ground
- public Transform groundCheck;
- public float checkRadius;
- public LayerMask WhatIsGround;
- private int extraJumps;
- public int extraJumpsValue;*/
- void Start () {
- /*extraJumps = extraJumpsValue;*/
- anim = GetComponent<Animator>();
- rb = GetComponent<Rigidbody2D>(); //sets rb to the physics
- healthDisplay.text = health.ToString();
- source = GetComponent<AudioSource>();
- }
- void FixedUpdate () { //When you are working with physics
- /*isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, WhatIsGround);*/
- input = Input.GetAxisRaw("Horizontal"); //Just GetAxis is smooth, Get axis raw is snappy
- //Makes player move, vector 2 variable
- rb.velocity = new Vector2(input * speed, rb.velocity.y); //input * speed is for x-axis, makes y do 0: rb.velocity.y
- //Calls flip function depending on if character is moving right or left
- if(facingRight == false && input > 0) {
- Flip();
- } else if(facingRight == true && input < 0) {
- Flip();
- }
- }
- private void Update() {
- /*Makes player jump
- if(isGrounded == true) {
- extraJumps = extraJumpsValue;
- }
- if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
- rb.velocity = Vector2.up * jumpForce;
- extraJumps--; //DONT FORGET THIS
- }
- else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true) {
- rb.velocity = Vector2.up * jumpForce;
- }*/
- //Choose between running or idle
- if (input !=0) {
- anim.SetBool("isRunning", true);
- }
- else {
- anim.SetBool("isRunning", false);
- }
- if (Input.GetKeyDown(KeyCode.Space) && isDashing == false) {
- speed += extraSpeed;
- isDashing = true;
- dashTime = startDashTime;
- }
- if (dashTime <= 0 && isDashing == true) {
- isDashing = false;
- speed -= extraSpeed;
- }
- else {
- dashTime -= Time.deltaTime;
- }
- }
- void Flip() {
- facingRight = !facingRight; //checks if the are right or left
- Vector3 Scaler = transform.localScale; //What makes them flip
- Scaler.x *= -1;
- transform.localScale = Scaler;
- }
- //Must be public to be accessed in enemy script
- public void TakeDamage(int damageAmount) {
- source.Play();
- health -= damageAmount;
- healthDisplay.text = health.ToString();
- if (health <= 0) {
- //Lose Screen
- losePanel.SetActive(true);
- //Destroy Player
- Destroy(gameObject); //destroy the object this script is on
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement