SHARE
TWEET

Enemy

a guest Nov 8th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Enemy : MonoBehaviour
  6. {
  7.     public int health;
  8.     public float speed, attackDistance;
  9.     public GameObject coin;
  10.     public GameObject deathAnimation;
  11.  
  12.     protected Animator anim;
  13.     protected bool facingRight = true;
  14.     protected Transform target;
  15.     protected float targetDistance;
  16.     protected Rigidbody2D rb2d;
  17.     protected SpriteRenderer sprite;
  18.  
  19.  
  20.  
  21.     // Start is called before the first frame update
  22.     void Awake()
  23.     {
  24.         anim = GetComponent<Animator>();
  25.         target = FindObjectOfType<Player>().transform;
  26.         rb2d = GetComponent<Rigidbody2D>();
  27.         sprite = GetComponent<SpriteRenderer>();
  28.  
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     protected virtual void Update()
  33.     {
  34.         targetDistance = transform.position.x - target.position.x;
  35.  
  36.     }
  37.  
  38.     protected void Flip()
  39.     {
  40.         facingRight = !facingRight;
  41.  
  42.         Vector3 scale = transform.localScale;
  43.         scale.x *= -1;
  44.         transform.localScale = scale;
  45.     }
  46.  
  47.     public void TookDamage(int damage)
  48.     {
  49.         health -= damage;
  50.         if(health <= 0)
  51.         {
  52.             //Instantiate(coin, transform.position, transform.rotation);
  53.             Instantiate(deathAnimation, transform.position, transform.rotation);
  54.  
  55.             //gameObject.SetActive(false);
  56.             Destroy(gameObject);
  57.  
  58.         }
  59.         else
  60.         {
  61.             StartCoroutine(TookDameageCoRoutine());
  62.         }
  63.     }
  64.  
  65.     IEnumerator TookDameageCoRoutine()
  66.     {
  67.         sprite.color = Color.red;
  68.         yield return new WaitForSeconds(0.1f);
  69.         sprite.color = Color.white;
  70.     }
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top