Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public ParticleSystem dust;
- public float speed;
- public float jumpForse;
- private Rigidbody2D rb;
- private bool facingRight = true;
- private bool isGrounded;
- public Transform groundCheck;
- public float checkRadius;
- public LayerMask whatIsGround;
- private int extraJumps;
- public int extraJumpsValue;
- public int maxHealth = 100;
- public int currentHealth;
- public HealthBar healthBar;
- Animator anim;
- public GameObject eatObject;
- public float endTime;
- private void Start()
- {
- extraJumps = extraJumpsValue;
- rb = GetComponent<Rigidbody2D>();
- anim = GetComponent<Animator>();
- currentHealth = PlayerPrefs.GetInt("life");
- healthBar.SetHealth(currentHealth);
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (this.CompareTag("Player") && collision.CompareTag("Eat"))
- {
- HealHelth(5);
- EatObject(collision);
- Destroy(collision.gameObject);
- }
- if (this.CompareTag("Player") && collision.CompareTag("Damage"))
- {
- TakeDamage(5);
- }
- }
- private void FixedUpdate()
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
- rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
- if (facingRight == false && Input.GetAxis("Horizontal") > 0)
- {
- Flip();
- }
- else if (facingRight == true && Input.GetAxis("Horizontal") < 0)
- {
- Flip();
- }
- }
- void Flip()
- {
- CreateDust();
- facingRight = !facingRight;
- Vector3 Scaler = transform.localScale;
- Scaler.x *= -1;
- transform.localScale = Scaler;
- }
- private void Update()
- {
- if (Input.GetAxis("Horizontal") == 0)
- {
- anim.SetInteger("Anim", 0);
- } else
- {
- anim.SetInteger("Anim", 1);
- }
- if (isGrounded == true)
- {
- extraJumps = extraJumpsValue;
- }
- if(Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
- {
- rb.velocity = Vector2.up * jumpForse;
- extraJumps--;
- anim.SetInteger("Anim", 2);
- CreateDust();
- }
- else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
- {
- rb.velocity = Vector2.up * jumpForse;
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- }
- }
- void CreateDust()
- {
- dust.Play();
- }
- void TakeDamage(int damage)
- {
- currentHealth -= damage;
- PlayerPrefs.SetInt("life", currentHealth);
- healthBar.SetHealth(currentHealth);
- }
- void HealHelth(int heal)
- {
- int nowhelth = currentHealth + heal;
- if (nowhelth > maxHealth)
- {
- healthBar.SetHealth(maxHealth);
- PlayerPrefs.SetInt("life", maxHealth);
- }
- else
- {
- currentHealth += heal;
- PlayerPrefs.SetInt("life", currentHealth);
- healthBar.SetHealth(currentHealth);
- }
- }
- void EatObject(Collider2D collision)
- {
- GameObject tempObject = Instantiate(eatObject, collision.gameObject.transform.position, Quaternion.identity);
- Destroy(tempObject, endTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment