Untitled
using System.Collections; using TMPro; using Unity.Mathematics; using UnityEngine; public class Enemy : MonoBehaviour { private bool dead = false; private float pv; private Animator animator; private GameObject damageTextPrefab; private Color myColor; private Canvas canvas; void Awake() { animator = GetComponent<Animator>(); damageTextPrefab = Resources.Load<GameObject>("Prefabs/damagePopUp"); canvas = FindFirstObjectByType<Canvas>(); } public void SetPv(float pv) { if(pv <0) { this.pv = 0; StartCoroutine(Die()); } else { this.pv = pv; } } public float GetPv() { return pv; } public void Hit(float degat) { if(degat > 499) {myColor = Color.yellow;} else if(degat >1499) {myColor = new Color(255,128,0);} else if(degat > 4999) {myColor = Color.red;} else{myColor = Color.grey;} StartCoroutine(ShowDamageText(degat,myColor)); if (animator != null) { animator.SetTrigger("Hit"); } pv -= degat; Debug.Log(pv); if(pv<=0){StartCoroutine(Die());} } IEnumerator Die() { if (animator != null) { animator.SetTrigger("Die"); } dead = true; yield return new WaitForSeconds(3f); Destroy(gameObject); } IEnumerator ShowDamageText(float degat, Color damageTextColor) { if (damageTextPrefab != null && dead == false) { GameObject damageText = Instantiate(damageTextPrefab,transform.position, Quaternion.identity, canvas.transform); TextMeshProUGUI textComponent = damageText.GetComponent<TextMeshProUGUI>(); if (textComponent != null) { textComponent.text = math.round(degat).ToString(); textComponent.color = damageTextColor; for (float i = 255f; i >= 0; i--) { textComponent.color = new Color(damageTextColor.r, damageTextColor.g, damageTextColor.b, i / 255f); yield return new WaitForSeconds(0.005f); } Destroy(damageText); } } } }
Leave a Comment