Guest User

Untitled

a guest
Feb 7th, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     public ParticleSystem dust;
  8.  
  9.     public float speed;
  10.     public float jumpForse;
  11.  
  12.     private Rigidbody2D rb;
  13.  
  14.     private bool facingRight = true;
  15.  
  16.     private bool isGrounded;
  17.     public Transform groundCheck;
  18.     public float checkRadius;
  19.     public LayerMask whatIsGround;
  20.  
  21.     private int extraJumps;
  22.     public int extraJumpsValue;
  23.  
  24.     public int maxHealth = 100;
  25.     public int currentHealth;
  26.  
  27.     public HealthBar healthBar;
  28.  
  29.     Animator anim;
  30.  
  31.     public GameObject eatObject;
  32.  
  33.     public float endTime;
  34.  
  35.     private void Start()
  36.     {
  37.         extraJumps = extraJumpsValue;
  38.         rb = GetComponent<Rigidbody2D>();
  39.         anim = GetComponent<Animator>();
  40.         currentHealth = PlayerPrefs.GetInt("life");
  41.         healthBar.SetHealth(currentHealth);
  42.     }
  43.  
  44.     private void OnTriggerEnter2D(Collider2D collision)
  45.     {
  46.    
  47.         if (this.CompareTag("Player") && collision.CompareTag("Eat"))
  48.         {
  49.             HealHelth(5);
  50.             EatObject(collision);
  51.             Destroy(collision.gameObject);
  52.         }
  53.  
  54.         if (this.CompareTag("Player") && collision.CompareTag("Damage"))
  55.         {
  56.             TakeDamage(5);
  57.         }
  58.     }
  59.  
  60.     private void FixedUpdate()
  61.     {
  62.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  63.  
  64.         rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
  65.  
  66.         if (facingRight == false && Input.GetAxis("Horizontal") > 0)
  67.         {
  68.             Flip();
  69.         }
  70.         else if (facingRight == true && Input.GetAxis("Horizontal") < 0)
  71.         {
  72.             Flip();
  73.         }
  74.  
  75.  
  76.     }
  77.  
  78.     void Flip()
  79.     {
  80.         CreateDust();
  81.         facingRight = !facingRight;
  82.         Vector3 Scaler = transform.localScale;
  83.         Scaler.x *= -1;
  84.         transform.localScale = Scaler;
  85.     }
  86.  
  87.     private void Update()
  88.     {
  89.         if (Input.GetAxis("Horizontal") == 0)
  90.         {
  91.             anim.SetInteger("Anim", 0);
  92.        
  93.  
  94.         } else
  95.         {
  96.             anim.SetInteger("Anim", 1);
  97.            
  98.         }
  99.         if (isGrounded == true)
  100.         {
  101.             extraJumps = extraJumpsValue;
  102.         }
  103.         if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
  104.         {
  105.             rb.velocity = Vector2.up * jumpForse;
  106.             extraJumps--;
  107.             anim.SetInteger("Anim", 2);
  108.             CreateDust();          
  109.         }
  110.         else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
  111.         {
  112.             rb.velocity = Vector2.up * jumpForse;
  113.         }
  114.         if (Input.GetKeyDown(KeyCode.Space))
  115.         {
  116.        
  117.         }
  118.     }
  119.  
  120.     void CreateDust()
  121.     {
  122.         dust.Play();
  123.     }
  124.  
  125.     void TakeDamage(int damage)
  126.     {
  127.         currentHealth -= damage;
  128.         PlayerPrefs.SetInt("life", currentHealth);
  129.         healthBar.SetHealth(currentHealth);
  130.     }
  131.  
  132.     void HealHelth(int heal)
  133.     {
  134.         int nowhelth = currentHealth + heal;
  135.        
  136.         if (nowhelth > maxHealth)
  137.         {
  138.             healthBar.SetHealth(maxHealth);
  139.             PlayerPrefs.SetInt("life", maxHealth);
  140.  
  141.         }
  142.         else
  143.         {
  144.             currentHealth += heal;
  145.             PlayerPrefs.SetInt("life", currentHealth);
  146.             healthBar.SetHealth(currentHealth);
  147.         }
  148.     }
  149.  
  150.     void EatObject(Collider2D collision)
  151.     {
  152.         GameObject tempObject = Instantiate(eatObject, collision.gameObject.transform.position, Quaternion.identity);
  153.         Destroy(tempObject, endTime);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment